In order to handle more complicated situation when we read data from a text file, I decided to write a new version, namely 2.0, of “reading data from text file”.
Put the code firstly :)
headers
your have to include and namepace
you have to use. #include <iostream>
#include <vector>
#include <fstream>
#include <sstream>
#include <string>
using namespace std;
#include <boost/algorithm/string.hpp>
using namespace boost::algorithm;
Line checker is used to check whether the given line contains specified number of values or not.
Firt parameter is the string includes values. The second is the specified or desired number of values in the previous string.
Because we usually desire the number of values in each txt line is equivalent. For instance :
Valid case showing below
1 2 3
4 5 6
and invalid case
1 2
3 4 5
And the code :
template<typename DT>
bool check_line_data(
const string &str,
const int num_cols
){
istringstream in_str(str);
int num_ele = 0;
DT tp_val;
while(in_str >> tp_val){
num_ele++;
}
return (num_ele==num_cols);
}
The Code !
template<typename DT>
int read_from_txt(
vector<DT> &data,
int &num_rows, int &num_cols,
const string &filename)
{
ifstream in_file;
istringstream in_str;
string line; // the string to store one line.
DT tp_var;
int k;
in_file.open(filename.c_str(), ios_base::in);
if (!in_file.is_open()){
cerr << "failed to open the file of <" << filename << ">" << endl;
data.clear();
num_rows = 0;
num_cols = 0;
return 1;
}
// read the first line with content and check number of column.
while (std::getline(in_file, line)){
trim(line);
if (line.size()==0)
continue;
in_str.str(line);
num_cols = 0;
in_str.clear();
while(in_str >> tp_var) {
num_cols++;
}
break;
}
if (0==num_cols){ // empty file
num_rows = 0;
data.clear();
return 1;
}
// read the remaining data and stored into a vector.
in_file.seekg(ifstream::beg);
num_rows = 0;
while(std::getline(in_file, line))
{
// remove black characters from the begining and ending of the line.
// the only function depends on BOOST!!!!
trim(line);
if (line.size()==0)
continue;
in_str.clear();
in_str.str(line); // ready to read the data.
if (check_line_data<DT>(line, num_cols)){
for (k = 0; k < num_cols; k++){
in_str >> tp_var;
data.push_back(tp_var);
}
num_rows++;
}
else{
data.clear();
num_rows = 0;
num_cols = 0;
return 2; // differet number of lines
}
}
in_file.close();
return 0;
}
vector\
Just Call the Last Function with correct arguments!!!
After serveral test case, what I think is right is the code is more robust than the first version.
It shows that the code has the ability to handle empty file
, invalid filename
, file with empty lines
, file with different number of columns
, etc.
If you have interest, please test the code~~
Enjoy it and good luck !!!!
原文:http://blog.csdn.net/bendanban/article/details/44600429