Presuming that is the data in your file, meaning you don't have to deal with quoted fields, then your job is very simple. To keep it fast, avoid parsing anything you don't need and overly-general solutions.
size_t col = 6 - 1; // Remember, stuff in C++ is zero-based
double default_value = 0.0;
vector <double> result;
ifstream f( "foo.csv" );
if (!f) fooey();
string s;
while (getline( f, s ))
{
size_t first = -1;
// Find the (col - 1)th comma. The next field will be the (col)th.
for (size_t n = 1; n < col; n++)
first = s.find( ',', first + 1 );
try // to read the column data
{
if (first != s.npos) first += 1;
result.push_back( strtod( s.substr( first ) ) );
}
catch (...)
{
result.push_back( default_value );
}
}
You can add stuff to skip empty lines and the like if you want. For example:
size_t col = 6 - 1; // Remember, stuff in C++ is zero-based
double default_value = 0.0;
vector <double> result;
ifstream f( "foo.csv" );
if (!f) fooey();
string s;
while (getline( f, s ))
{
// If you have comments or empty lines you should skip them here.
// For example, this skips any empty lines with no spaces
if (s.empty()) continue;
// This gets ready for the next step and skips empty lines with spaces
size_t first = s.find_first_not_of( " \f\r\t\v" );
if (first == s.npos) continue;
first -= 1;
// Find the (col - 1)th comma. The next field will be the (col)th.
for (size_t n = 1; n < col; n++)
first = s.find( ',', first + 1 );
try // to read the column data
{
if (first != s.npos) first += 1;
result.push_back( strtod( s.substr( first ) ) );
}
catch (...)
{
result.push_back( default_value );
}
}
Thank you very much!! But I need to arrange this data set in a 'tab' separated format, I could not understand from the above code how to do that.
My original file also has a header row like,