C++: follow-up question on reading from an external file using getline(). How to get a subset of the file data?

StackOverflow https://stackoverflow.com/questions/5005673

Question

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.

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.

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.

Here's the code plus an example of a file (let's call it "test") that I want to get data from.


Edit: 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:

if( line == "vectorTwo" )
{
      // The while statement and its contents
}

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.


//Here are the contents of the example file

vectorOne // Identifier for subset of data for one vector

'1' '2' '3'

vectorTwo // How would I get this one vector? Or any other one vector?

'4' '5' '6'

vectorThree // Identifier for subset of data for another vector

'7' '8' '9'

// 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?

#include <fstream>
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <iterator>

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 << "Error opening file test.dat\n";

    std::vector<int> 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 >> i;
        if (myStream.fail())
            break;
        numbers.push_back(i);
    }
    std::copy(numbers.begin(), numbers.end(),
              std::ostream_iterator<int>(std::cout, "\n"));
}
Was it helpful?

Solution

When you reach the start of a block just read to the end of the line into a string, then parse the string.

std::getline( theStream, block, '\n' );
std::istringstream blockStream( block );
std::getline( blockStream, line, '\'' ); // etc
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top