Hello,

In the past I had written the following function to parse a string that was tab delimited:

void parseLine(string s)
{
        string fid, src, dst, cap_e, cap_f, fare_e, fare_f;
        istringstream isstream (s);

        getline(isstream,fid,'|');
        getline(isstream,src,'|');
        getline(isstream,dst,'|');
        getline(isstream,cap_e,'|');
        getline(isstream,cap_f,'|');
        getline(isstream,fare_e,'|');
        getline(isstream,fare_f,'|');
}

Now I need to parse a string where I dont know in advance how many fields are there. How can I do that as I dont know how long the string would be.

Example:

New Flight A33333
Action Takeoff 1230 Land 1430

I want to do something like -
If the first word is New then check the 3rd word
If the first word is Action and second word is takeoff then check the 3rd word for takeoff time and 5th word for landing time etc....

Thanks
-k

If you don't know how many strings, then use a std::vector<std::string> instead of named variables.

Something like

vector<string> fields;
string temp;
while ( getline(isstream,temp,'|') {
  fields.push_back(temp);
}

Then check fields[0] for whatever.

If you don't know how many strings, then use a std::vector<std::string> instead of named variables.

Something like

vector<string> fields;
string temp;
while ( getline(isstream,temp,'|') {
  fields.push_back(temp);
}

Then check fields[0] for whatever.

Thanks Salem. I will try that out

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.