title: How to read-write into/from text file with comma separated values
body: <p>How do I read data from a file if my file is like this with comma separated values</p>
  
  <pre><code>1, 2, 3, 4, 5\n
  6, 7, 8, 9, 10\n
  \n
  </code></pre>
  
  <p>and after reading the file, I want to write the data back into other file as same format above.</p>
  
  <p>I can get total number of lines, using</p>
  
  <pre><code>string line;
  while(!file.eof()){
        getline(file,line);
        numlines++;
      }
      numline--; // remove the last empty line
  </code></pre>
  
  <p>but how can I know total number of digits in a row/line ??</p>
  
  <p>I also have vector of ints to store the data.
  So, I want to read the first line and then count total number of elements in that line, here 5 (1,2,3,4,5) and store them in array/vector, and read next line and store them in vector again and so on till I reach EOF.</p>
  
  <p>Then, I want to write the data to file, again, I guess this will do the job of writing data to file,</p>
  
  <pre><code>numOfCols=1;
  for(int i = 0; i &lt; vector.size(); i++)
  {
      file &lt;&lt; vector.at(i);
      if((numOfCols&lt;5) file &lt;&lt; ",";//print comma (,)
      if((i+1)%5==0)
      {
                    file &lt;&lt; endl;//print newline after 5th value
                    numOfCols=1;//start from column 1 again, for the next line
      }
      numOfCols++;
  }
  file &lt;&lt; endl;// last new line
  </code></pre>
  
  <p>So, my main problem is how to read the data from file with comma separated values ??</p>
  
  <p>Thanks</p>