简体   繁体   English

如何获取getline字符串中的某个单词?

[英]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? 有人怎么能在getline方法的字符串中只得到某个单词? For example: 例如:

Test.txt: 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 )? 我该如何编码,以便当我找到@@Paul它仅在双标签( \\t\\t )之后使用字符( [GET THIS] )?

this is definition of getline function: istream& getline (istream& is, string& str, char delim); 这是getline函数的定义:istream&getline(istream&is,string&str,char delim);

Delim is a delimitier which stops getline from reading the stream after to the given character. Delim是一个定界符,它阻止getline从读取流之后再读取给定字符。 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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