title: How to read a stream with comma separated values in C++?
body: <p>I would like to emphasize on a fundamental question below:</p>
  
  <p>Assume you have a CSV file and fill cells with Input Headers </p>
  
  <p>The code should read this from .csv file and write the results into .csv file. It is nice to also code output total number of cases, plus average of cases. Here is a taken sample form SO and I would like to see how this can be efficiently adopted to complete this basic example. </p>
  
  <pre><code>   void create() 
  { 
      // file pointer 
      fstream fout; 
  
      // opens an existing csv file or creates a new file. 
      fout.open("reportcard.csv", ios::out | ios::app); 
  
      cout &lt;&lt; "Enter the details of 5 students:"
          &lt;&lt; " roll name maths phy chem bio"; 
      &lt;&lt; endl; 
  
      int i, roll, phy, chem, math, bio; 
      string name; 
  
      // Read the input 
      for (i = 0; i &lt; 5; i++) { 
  
          cin &gt;&gt; roll 
              &gt;&gt; name 
              &gt;&gt; math 
              &gt;&gt; phy 
              &gt;&gt; chem 
              &gt;&gt; bio; 
  
          // Insert the data to file 
          fout &lt;&lt; roll &lt;&lt; ", "
              &lt;&lt; name &lt;&lt; ", "
              &lt;&lt; math &lt;&lt; ", "
              &lt;&lt; phy &lt;&lt; ", "
              &lt;&lt; chem &lt;&lt; ", "
              &lt;&lt; bio 
              &lt;&lt; "\n"; 
      } 
  } 
  </code></pre>
  
  <p>Also, Read a particular record</p>
  
  <pre><code>void read_record() 
  { 
  
      // File pointer 
      fstream fin; 
  
      // Open an existing file 
      fin.open("reportcard.csv", ios::in); 
  
      // Get the roll number 
      // of which the data is required 
      int rollnum, roll2, count = 0; 
      cout &lt;&lt; "Enter the roll number "
          &lt;&lt; "of the student to display details: "; 
      cin &gt;&gt; rollnum; 
  
      // Read the Data from the file 
      // as String Vector 
      vector&lt;string&gt; row; 
      string line, word, temp; 
  
      while (fin &gt;&gt; temp) { 
  
          row.clear(); 
  
          // read an entire row and 
          // store it in a string variable 'line' 
          getline(fin, line); 
  
          // used for breaking words 
          stringstream s(line); 
  
          // read every column data of a row and 
          // store it in a string variable, 'word' 
          while (getline(s, word, ', ')) { 
  
              // add all the column data 
              // of a row to a vector 
              row.push_back(word); 
          } 
  
          // convert string to integer for comparision 
          roll2 = stoi(row[0]); 
  
          // Compare the roll number 
          if (roll2 == rollnum) { 
  
              // Print the found data 
              count = 1; 
              cout &lt;&lt; "Details of Roll " &lt;&lt; row[0] &lt;&lt; " : \n"; 
              cout &lt;&lt; "Name: " &lt;&lt; row[1] &lt;&lt; "\n"; 
              cout &lt;&lt; "Maths: " &lt;&lt; row[2] &lt;&lt; "\n"; 
              cout &lt;&lt; "Physics: " &lt;&lt; row[3] &lt;&lt; "\n"; 
              cout &lt;&lt; "Chemistry: " &lt;&lt; row[4] &lt;&lt; "\n"; 
              cout &lt;&lt; "Biology: " &lt;&lt; row[5] &lt;&lt; "\n"; 
              break; 
          } 
      } 
      if (count == 0) 
          cout &lt;&lt; "Record not found\n"; 
  } 
  
  
    [1]: https://i.stack.imgur.com/q6VfZ.png
  </code></pre>