title: Reading csv with fstream, and only fstream
body: <p>I have seen other answers on this matter, but all of them deal with a <code>std::stringstream</code>, or a temporary <code>char</code> or <code>std::string</code> array, various other sorts of external libraries, but I would like to try using only the <code>fstream</code> header, to try to read a file that has only numbers, both <code>char</code> and <code>short</code>, together with <code>float</code>, separated by commas, forming more than one lines of text; some may be arrays, or vectors. Example:</p>
  
  <pre><code>1,1.1,11.1,11
  2,2.2,22.2,22
  3,3.3,33.3,33
  ...
  </code></pre>
  
  <p>The order is known, since each line follows the variables from a <code>struct</code>. The number of lines may vary, but, for now, let's assume it is also known. Also for the sake of example, let's only consider this order, and these types:</p>
  
  <pre><code>int, double, double, int
  </code></pre>
  
  <p>Going along with a piece of code I have seen, I tried this simplistic (and, most probably, naive) approach:</p>
  
  <pre><code>int a, d;
  double b, c;
  char fileName {"file.txt"};
  std::fstream fs {fileName};
  if(!fs.is_open())
      // open with fs.out, write some defaults; this works, no need to mention
  else
  {
      char comma;
      while(fs.getline(fileName, 100, '\n'))
      {
          fs &gt;&gt; a &gt;&gt; comma &gt;&gt; b &gt;&gt; comma &gt;&gt; c &gt;&gt; comma &gt;&gt; d;
          std::cout &lt;&lt; 2*a &lt;&lt; ", " &lt;&lt; 2*b &lt;&lt; ", " &lt;&lt; 2*c &lt;&lt; ", " &lt;&lt; 2*d &lt;&lt; '\n';
      }
  }
  </code></pre>
  
  <p>If the file has the three lines above, plus a terminating <code>\n</code>, it outputs this:</p>
  
  <pre><code>4, 4.4, 44.4, 44
  6, 6.6, 66.6, 66
  6, 6.6, 66.6, 66
  *** stack smashing detected ***: &lt;unknown&gt; terminated
  Aborted (core dumped)
  </code></pre>
  
  <p>If I add a <code>\n</code> at the beginning of the file, it outputs:</p>
  
  <pre><code>2, 2.2, 22.2, 22
  4, 4.4, 44.4, 44
  6, 6.6, 66.6, 66
  6, 6.6, 66.6, 66
  </code></pre>
  
  <p>If I remove the last <code>\n</code>, it works as intended. I have a few questions:</p>
  
  <ol>
  <li><p>What else can I do when writing the file besides adding a beginning <code>\n</code> and not inserting a terminating one in order to work as intended?</p></li>
  <li><p>If the number of variables is longer, say 100 per line, what can I do to avoid going 'round the Earth with <code>fs &gt;&gt; a &gt;&gt; c &gt;&gt; ...</code>?</p></li>
  <li><p>If I only need to read a specific line, or only a few, one method would probably be counting the occurences of <code>\n</code>, or the lines, somehow. How could I do that?</p></li>
  </ol>
  
  <p>(edit)</p>
  
  <ol start="4">
  <li>Not lastly, as the title mentions, is it possible to do it without involving other headers, only with <code>fstream</code> (as it currently is, for example)?</li>
  </ol>