C++ 如何从c+中的文本文件体中获取特定的单词和行+;

C++ 如何从c+中的文本文件体中获取特定的单词和行+;,c++,ifstream,C++,Ifstream,当运行这段代码时,当我只需要包含单词的特定行时,就会输出整个文件。e、 g查找单词paris并显示所有行,而不是特定行 我已经试过上网,发推,但是没有用,我觉得这是问题的一部分,但我不确定 void choice1() { string tweet; ifstream infile; infile.open("sampleTweets.csv"); if (infile.good()) { while (!infile.eof()) {

当运行这段代码时,当我只需要包含单词的特定行时,就会输出整个文件。e、 g查找单词paris并显示所有行,而不是特定行

我已经试过上网,发推,但是没有用,我觉得这是问题的一部分,但我不确定

void choice1() {
    string tweet;
    ifstream infile;
    infile.open("sampleTweets.csv");

    if (infile.good()) {
        while (!infile.eof()) {
            getline(infile,tweet);
            if (tweet.find("Paris") != tweet.length()) {
                cout << "Found Paris in the line" << tweet << endl;
            }
        }
    }
}
void选项1(){
字符串tweet;
河流充填;
infle.open(“sampleTweets.csv”);
if(infle.good()){
而(!infle.eof()){
getline(内嵌、推特);
if(tweet.find(“Paris”)!=tweet.length(){

cout标准说
find
方法返回
npos
,如果没有找到。您可以参考

因此,条件行应为:

if(tweet.find(“Paris”)!=string::npos){

正确用法如下所示:

if (tweet.find("Paris") != std::string::npos) {
}
请参考正确用法。

更喜欢
while(getline(infle,tweet))
而不是
while(!infle.eof()){getline(infle,tweet)太长了,读不下去了。在读之前,测试正确性是允许读的数据不被检查的。你需要做的是读取数据。检查数据是否被读取。使用数据。<代码>(GETLIN(…))
读取并检查,仅当
getline
成功时才进入循环。