简体   繁体   中英

Is there a way to getline just part of a line?

I couldn't come up with a better title, but let me explain.

I have a file like this

potions.txt potions ingredients.txt ingredients

INSERT((Red Mountain Flower|0.1|2|Yes|No|Mountains,Forests),ingredients)
INSERT((Abecean Longfin|0.5|15|No|Yes|Rivers,Lakes),ingredients)
INSERT((48|Glibness|Fortify|+20 Speechcraft for 60 seconds.|96|None|None),potions)
UPDATE((Abecean Longfin|0.5|15|No|Yes|Rivers,Lakes,Swamps),ingredients)
UPDATE((205|Minor Healing|Health|Restore 25 points of Health.|17|Blue Mountain Flower|Charred Skeever Hide),potions)
UPDATE((206|Healing|Health|Restore 50 points of Health.|36|Blue Mountain Flower|Swamp Fungal Pod),potions)
SELECT((9|*|*|*|*|*|*),potions)
INSERT((Purple Mountain Flower|0.1|2|Yes|No|Mountains,Forests),ingredients)

I am trying to parse the file to store the proper things into the proper variable.

So, I tried writing

for(int i = 0; i < num_of_lines; i++)
{
        getline(inputFile, insert, '(');
        if(insert == "INSERT")
        {
            cout << insert << endl;
        }
}

And I immediately know my problem. When the for loop continues, the order it will read things in is

(
Red Mountain Flower|0.1|2|Yes|No|Mountains,Forests),ingredients)INSERT(
(Abecean Longfin|0.5|15|No|Yes|Rivers,Lakes),ingredients)INSERT(

Meaning it will never get another "insert" to read in so i'll never have access to it in order to parse the file further.

Is there a way to getline just part of a line so that If the string matches, I can continue to parse the file? I've tried find functions, I've tried string compare functions, but I can't seem to get anything to work. Any suggestions on how i can solve this would be appreciated.

The input is obviously line-based, so read it line by line and then parse those lines:

for(int i = 0; i < num_of_lines; i++)
{
        getline(inputFile, lineText);
        std::istringstream line(lineText);

        // Now, work with `line` as your stream
        getline(line, insert, '(');
        if(insert == "INSERT")
        {
            cout << insert << endl;
        }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM