Reading csv file

I have a CSV file like this

1896,1,0,0,0.0074,0.0214
1896,2,0,0,0.0104,0.0302
1896,3, 0,0,0.0078,0.0225
1896,4,0,0,0.0088,0
1896,5,0,0,0.0068,0

I need to extract the data in column 6 i.e 0.0074, 0.0104 ....
I am getting the problem with the ','....any help!!!

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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 );
    }
  }

Hope this helps.
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,

yr,m,z_score,p_value,n_term
I have saved the file in the tab separated format. thanks for the help
Wait, I'm confused now.

First you asked to read the data from file.

Now you are asking to write the data? Did you figure that out?
I have solved the problem!! I read each line upto the ',' and break it there. Then same them in the new file separating with '\t'.
Topic archived. No new replies allowed.