简体   繁体   中英

How to get a certain word in string of getline?

How can someone get only a certain word in a string of a getline method? For example:

Test.txt:

 hi guys
 im @@Paul \t\t [GET THIS]

string line;
string word;
ifstream file ("test.txt");

if (file.is_open()) {
    while (getline(file, line)) {
        if (line.find("@@Paul") != string::npos) {
            strcpy(word, line.c_str());
        }
    }
}

How can I code it so when I find @@Paul it only takes the characters ( [GET THIS] ) after the double tab ( \\t\\t )?

this is definition of getline function: istream& getline (istream& is, string& str, char delim);

Delim is a delimitier which stops getline from reading the stream after to the given character. so you can do something like this:

string trash;
string properline;

getline(file, trash, '\t');
getline(file, trash, '\t'); //second tab, still trash
getline(file, properline);

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