title: C++ Reading an multiline file with lines with arbitary lengths and format without using a stringstream
body: <p>I have an input stream with the following lines:</p>
  
  <pre><code># &lt;int&gt; &lt;int&gt;
  &lt;some_data_type&gt; &lt;some_data_type&gt; &lt;some_data_type&gt; ..... &lt;some_data_type&gt;
  &lt;some_data_type_1&gt; &lt;some_data_type_2&gt; &lt;some_data_type_3&gt; &lt;some_data_type_1&gt; &lt;some_data_type_2&gt; &lt;some_data_type_3&gt; .... &lt;some_data_type_1&gt; &lt;some_data_type_2&gt; &lt;some_data_type_3&gt; 
  </code></pre>
  
  <p>In the above stream all three lines are different and have to be parsed differently. Currently,I am using a reading method as follows:</p>
  
  <pre><code>void reader( std::istream &amp; is, DataStructure &amp; d ){
    std::string line;
    getline(is,line);
    std::stringstream s(line);
    //parse line 1
  
    getline(is,line);
    std::stringstream line2(line);
    //parse line 2
  
    getline(is,line);
    std::stringstream line3(line);
    //parse line 3
  
   }
  </code></pre>
  
  <p>Now the idea is not to make use of <code>std::stringstream</code> at all, as a line can arbitarily large and we donot want to load everything into memory twice. So, it would be better if it was possible to read from the input stream directly into the user given datastructure d. </p>
  
  <p>An idea is to make use of <code>std::istream_iterator</code> but unfortunately the different lines have different parsing needs. For example, in the last line, three elements from the stream together constitute a single data element.</p>
  
  <p>The only idea that seems plausible to me at this moment is to handle the stream buffer directly. It would be great if anyone could recommend a better way of doing this. </p>
  
  <p>NOTE: <strong>Cannot</strong> make use of a tertiary data structure like <code>std::stringstream</code>. It is essential to read from the stream directly into the user provided data structure. </p>
  
  <p>EDIT: Please note we are only allowed a single pass over the file. </p>