K2SO Ghost Spirit
K2SO Ghost Spirit

Reputation: 1291

How do I parse a text file with unknown amount of variables on a line?

I am reading in a text file. The first number is the part #. The Next is the part name. Followed by its subparts-which can be anything from nothing to a lot of different numbers. How do I parse this out with an unknown amount of variables I would like to read in? Thanks!

For example:

12 Engine 11 14 39 26

11 Fan 9 6

9 Fanblade

6 Bearing

14 Compressor 11 6

39 Combustor 65 63

65 nozzle

63 Fuel-Line

26 Turbine 9 6 77

77 Gear

And what I have been using but obviously only grabbing the first number after the part name:

while(getline(file_in, line)) {
    istringstream strm;

     strm.str(line);

    string id;
    string name;
    string parent;

    strm >> id;
    strm >> name;
    strm >> parent;
    cout << "Got ID "<<id<<" Name "<<name<<" Parent "<<parent<<endl;

}

Upvotes: 0

Views: 105

Answers (1)

Mats Petersson
Mats Petersson

Reputation: 129364

Something like this:

vector<string> parents;

while(strm >> parent)
{
   parents.push_back(parent);
}

Upvotes: 1

Related Questions