【问题标题】:Read two lines at a time from a .txt file - C++ getline/ streams?从 .txt 文件中一次读取两行 - C++ getline/streams?
【发布时间】:2013-10-14 21:40:06
【问题描述】:

我正在使用下面的代码来读取文件,搜索给定的字符串并显示该行。但我想将immediate next line 阅读到我在文件的字符串搜索中找到的内容。我可以增加行号以获取下一行,但我需要在文件上再次使用getline 吗?

这是我的代码:

#include <string>
#include <iostream>
#include <fstream>

    int main()
    {
        std::ifstream file( "data.txt" ) ;
        std::string search_str = "Man" ;
        std::string line ;
        int line_number = 0 ;
        while( std::getline( file, line ) )
        {
            ++line_number ;

            if( line.find(search_str) != std::string::npos )
            {
                std::cout << "line " << line_number << ": " << line << '\n' ;
                std::cout << ++line_number; // read the next line too
            }

        }

        return (0);
    }

这是我的文件的内容:

Stu
Phil and Doug
Jason
Bourne or X
Stephen
Hawlkings or Jonathan
Major
League or Justice
Man
Super or Bat

【问题讨论】:

  • 我不确定你想要什么。是只显示匹配后的下一行还是匹配的行和下一行?
  • 是的特洛伊,匹配的行和紧接的下一行...

标签: c++ string file stream getline


【解决方案1】:

您不需要再次调用std::getline,但您需要一个标志来避免它:

#include <string>
#include <iostream>
#include <fstream>

int main()
{
    std::ifstream file( "data.txt" ) ;
    std::string search_str = "Man" ;
    std::string line ;
    int line_number = 0 ;
    bool test = false;
    while(std::getline(file, line))
    {
        ++line_number;
        if (test)
        {
            std::cout << "line " << line_number << ": " << line << '\n' ;
            break;
        }

        if( line.find(search_str) != std::string::npos )
        {
            std::cout << "line " << line_number << ": " << line << '\n' ;
            test = true;
        }

    }

    return (0);
}

【讨论】:

    【解决方案2】:

    是的,您需要getline 函数来读取下一行。

        while( file && std::getline( file, line ) )
        {
            ++line_number ;
    
            if( line.find(search_str) != std::string::npos )
            {
                std::cout << "line " << line_number << ": " << line << '\n' ;
                std::cout << ++line_number; // read the next line too
                std::getline(file, line);  // then do whatever you want.
    
            }
    
        }
    

    请注意while 子句中file 的用法,这很重要。 istream 对象可以被评估为布尔值,相当于 file.good()。您要检查状态的原因是第二个getline() 函数可能会到达文件末尾并引发异常。您还可以在第二次getline 调用之后添加检查,如果!file.good() 则添加break

    std::getline(file, line);  // then do whatever you want.
    if(line.good()){
       // line is read stored correctly and you can use it
    }
    else{
      // you are at end of the file and line is not read
      break;
    }
    

    那么就不需要检查了。

    【讨论】:

    • 我不确定您是否不小心重用了相同的变量名,但file 因为输入流和布尔标志要么不正确(因为名称冲突)或不必要(如 getline返回ostream&amp;)。当他找到结果时,他可以简单地设置一个布尔标志,然后在下一个循环中(在读取该行之后)测试它并跳出循环。
    【解决方案3】:

    您需要创建一个新的bool 标志变量,当您找到匹配项时设置该变量,然后在找到匹配项后再次循环,以便获得下一行。测试标志以确定您是否在上一个循环中找到了匹配项。

    【讨论】:

    • 你真的应该在得到一些指导后自己尝试解决这些问题。否则你不会学到任何东西:无论如何请看这里:coliru.stacked-crooked.com/a/d4b080eec491313a
    • 嘿,特洛伊,我正在使用 Zac 所说的以下代码,这是正确的还是任何内存泄漏或任何错误? - 请在这里查看:ideone.com/V2E4g3
    猜你喜欢
    • 2021-12-05
    • 2021-12-28
    • 2018-11-01
    • 1970-01-01
    • 2016-07-29
    • 2018-06-21
    • 1970-01-01
    • 2017-10-02
    • 1970-01-01
    相关资源
    最近更新 更多