Note that there are some explanatory texts on larger screens.

plurals
  1. POC++: follow-up question on reading from an external file using getline(). How to get a subset of the file data?
    primarykey
    data
    text
    <p>I need to read in numbers from an external file and store them in a vector of ints. I can do this now thanks to Howard Hinnant and wilhelmtell, who patiently helped figure out why my coding was not working yesterday.</p> <p>I have been trying to figure out how to incorporate an additional feature into the code, but I have exhausted my knowledge of streams and would appreciate some advice.</p> <p>I want to be able to deal with files containing many sets of data. Is it possible to extract only certain data from the file into a vector? I want to end up with several vectors that contain data from different parts of the file. I searched online, but have not seen any mention of this. </p> <p>Here's the code plus an example of a file (let's call it "test") that I want to get data from.</p> <hr> <p><strong>Edit:</strong> <em>I edited the code based on CashCow's advice. I can now get a block out of the data file. But I don't know how to get the block I want. If I run the code as it is, I get a vector that contains the elements 2,5,8 (this is not what I want). To get vectorTwo (4,5,6 in the example I made), I tried adding this around the while statement:</em></p> <pre><code>if( line == "vectorTwo" ) { // The while statement and its contents } </code></pre> <p>It did not work. I did not get any results from running the code (it compiled though). Can anyone see what the problem is? I figured I could use this statement to search for the header for the block of data I need.</p> <hr> <p>//Here are the contents of the example file</p> <p>vectorOne // Identifier for subset of data for one vector</p> <p>'1' '2' '3'</p> <p><strong>vectorTwo</strong> <strong><em>// How would I get this one vector? Or any other one vector?</em></strong> </p> <p>'4' '5' '6'</p> <p>vectorThree // Identifier for subset of data for another vector</p> <p>'7' '8' '9'</p> <p>// Code: The '\'' character is the line delimiter. Everything is ignored up to the first ' and then everything until the next ' is part of a number. This continue until the logic fails (end of file). How can I get it to stop at the end of a data block instead?</p> <pre><code>#include &lt;fstream&gt; #include &lt;iostream&gt; #include &lt;vector&gt; #include &lt;string&gt; #include &lt;sstream&gt; #include &lt;iterator&gt; int main() { std::string line; std::string block; // Edited, from CashCow const std::string fileName = "test.dat"; std::ifstream theStream( fileName.c_str() ); if( ! theStream ) std::cerr &lt;&lt; "Error opening file test.dat\n"; std::vector&lt;int&gt; numbers; // This code is written for one vector only. There would be three vectors for the example file I provided above; one for the vectorOne data in the file, and so on. while (true) { // get first ' std::getline(theStream, line, '\''); // read until second ' std::getline(theStream, line, '\''); std::istringstream myStream( line ); std::getline( theStream, block, '\n' ); // Edited, from CashCow std::istringstream blockstream( block ); // Edited, from CashCow std::getline(blockstream, line, '\''); // Edited, from CashCow int i; myStream &gt;&gt; i; if (myStream.fail()) break; numbers.push_back(i); } std::copy(numbers.begin(), numbers.end(), std::ostream_iterator&lt;int&gt;(std::cout, "\n")); } </code></pre>
    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