【问题标题】:How to use getline to print the correct line from txt file如何使用 getline 从 txt 文件中打印正确的行
【发布时间】:2019-09-05 02:49:35
【问题描述】:

我正在尝试创建一个程序,它将在给定的目录 txt 文件中搜索某个字符串。找到该字符串后,程序将仅将 txt 文件中该行的信息打印到屏幕上。我的代码能够检查字符串是否在文件中,但不是只打印它包含的行,而是打印除我想要的行之外的所有内容。

while(!inF.eof()){
    getline(inF, hold);
    if(hold.find(crnS)){
        isFound = 1;
        cout << hold << endl; 
    }                          
}

【问题讨论】:

  • while(getline(inF, hold))

标签: c++ getline


【解决方案1】:

你的代码有两个问题:

改用这个:

while (getline(inF, hold)) {
    if (hold.find(crnS) != string::npos) {
        isFound = 1;
        cout << hold << endl;
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-29
    • 1970-01-01
    相关资源
    最近更新 更多