Note that there are some explanatory texts on larger screens.

plurals
  1. POC++: reading data from an external file; problem with my code for stopping read before end of file
    primarykey
    data
    text
    <p>To use code I have written for performing calculations, I need to read in data (numbers and strings) from an external text file and store them in vectors of either strings or ints/doubles. I have written a template function for doing this. CashCow, Howard Hinnant, and wilhelmtell kindly helped with a previous problem. </p> <p>The function seems to work fine for ints/doubles, but I have a problem with string data.</p> <p>I need data from ONE line of my external file to go into a vector, but the function reads in multiple lines. Here's what I mean. Let's say this is what is in the external text file (below):</p> <hr> <p>vectorOne // Identifier for subset of data for one vector</p> <p>'1' '2' '3' // These values should go into one vector, (vectorOne)</p> <p>vectorTwo // Identifier for subset of data for another vector (vectorTwo)</p> <p>'4' '5' '6' // These values should go into a different vector</p> <p>vectorThree // Identifier for subset of data for another vector (vectorThree)</p> <p>'7' '8' '9' // These values should go into a different vector</p> <hr> <p>If I look for a data subset identifier/label (like vectorOne), I want only the data on the next line to go into my result vector. The problem is that ALL data below the identifier/label are ending up in the result vector. So if vectorTwo is what I want, I expect my result vector to contain the elements, "4, 5, 6." But intead, it contains 4 to 9. In my code (below), I thought that the line:</p> <pre><code>while ( file.get() != '\n' ); </code></pre> <p>ensured that the read would stop at a newline (i.e., after each line of data).</p> <p>I would be very grateful for any suggestions as to what is going wrong. </p> <p>Here's the code (for clarity, I configured it for strings):</p> <pre><code>#include &lt;algorithm&gt; #include &lt;cctype&gt; #include &lt;istream&gt; #include &lt;fstream&gt; #include &lt;iostream&gt; #include &lt;vector&gt; #include &lt;string&gt; #include &lt;sstream&gt; #include &lt;iterator&gt; using namespace std; template&lt;typename T&gt; void fileRead( std::vector&lt;T&gt;&amp; results, const std::string&amp; theFile, const std::string&amp; findMe, T&amp; temp ) { std::ifstream file( theFile.c_str() ); std::string line; while( std::getline( file, line ) ) { if( line == findMe ) { do{ std::getline( file, line, '\'' ); std::getline( file, line, '\''); std::istringstream myStream( line ); myStream &gt;&gt; temp; results.push_back( temp ); } while ( file.get() != '\n' ); } } } int main () { const std::string theFile = "test.txt"; // Path to file const std::string findMe = "labelInFile"; std::string temp; std::vector&lt;string&gt; results; fileRead&lt;std::string&gt;( results, theFile, findMe, temp ); cout &lt;&lt; "Result: \n"; std::copy(results.begin(), results.end(), std::ostream_iterator&lt;string&gt;(std::cout, "\n")); return 0; } </code></pre> <p>Thanks</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload