Reputation: 896
I wan t to read a large CSV file in c++, but reading in a line by line manner is too slow for me (about 5M records). As I'm not sure about the delimiter in the file (, or space), I read the file char by char, convert to string, split all values and populate the array by converted values to doubles. My code is shown below, but it fails to read '\n' and ' ' chars? Would you please help me how can I read them. If there exist any faster and reliable way to read.
bool readPtFast(istream *dataIn, ANNpointArray &p) // read point (false on EOF)
{
std::istream_iterator<char> begin(*dataIn), end;
std::vector<char> in(begin, end);
std::string wholeFileString(in.begin(), in.end()); // Not all chars are read!!
std::vector<std::string> split_values_string;
std::vector<double> split_values_double;
boost::split(split_values_string, wholeFileString, boost::is_any_of("\n\r,;\t "));
if (split_values_string.size()!=NRecords*NDims) {
cerr << "Error reading file. I expected " << NRecords*NDims << " values, but I found " << split_values_string.size() << "records.\a";
getchar();
return false;
}
std::transform(split_values_string.begin(), split_values_string.end(),
std::back_inserter(split_values_double),
boost::lexical_cast<double, std::string>);
std::copy(split_values_double.begin(),split_values_double.end(),*p);
return true;
}
Upvotes: 0
Views: 217
Reputation: 454
By default, the std::istream_iterator
will skip whitespace. Perhaps you could use the manipulator std::noskipws
as described here: http://www.cplusplus.com/reference/ios/noskipws/ or perhaps the stream.unsetf(std::skipws)
method
Upvotes: 1