title: Fast, Simple CSV Parsing in C++
body: <p>I am trying to parse a simple CSV file, with data in a format such as:</p>
  
  <pre><code>20.5,20.5,20.5,0.794145,4.05286,0.792519,1
  20.5,30.5,20.5,0.753669,3.91888,0.749897,1
  20.5,40.5,20.5,0.701055,3.80348,0.695326,1
  </code></pre>
  
  <p>So, a very simple and fixed format file. I am storing each column of this data into a STL vector. As such I've tried to stay the C++ way using the standard library, and my implementation within a loop looks something like:</p>
  
  <pre><code>string field;
  getline(file,line);
  stringstream ssline(line);
  
  getline( ssline, field, ',' );
  stringstream fs1(field);
  fs1 &gt;&gt; cent_x.at(n);
  
  getline( ssline, field, ',' );
  stringstream fs2(field);
  fs2 &gt;&gt; cent_y.at(n);
  
  getline( ssline, field, ',' );
  stringstream fs3(field);
  fs3 &gt;&gt; cent_z.at(n);
  
  getline( ssline, field, ',' );
  stringstream fs4(field);
  fs4 &gt;&gt; u.at(n);
  
  getline( ssline, field, ',' );
  stringstream fs5(field);
  fs5 &gt;&gt; v.at(n);
  
  getline( ssline, field, ',' );
  stringstream fs6(field);
  fs6 &gt;&gt; w.at(n);
  </code></pre>
  
  <p>The problem is, this is extremely slow (there are over 1 million rows per data file), and seems to me to be a bit inelegant. Is there a faster approach using the standard library, or should I just use stdio functions? It seems to me this entire code block would reduce to a single fscanf call.</p>
  
  <p>Thanks in advance!</p>