简体   繁体   中英

Read two lines at a time from a .txt file - C++ getline/ streams?

I am using the below code to read a file, search for the given string and display that line. But I want to read the immediate next line to what I've found in string search of the file. I can increment the line number to get the next line, but do I need to use getline again on the file?

Here is my code:

#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);
    }

Here is the contents of my file:

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

You do not need another std::getline call, but you will need a flag to avoid it:

#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);
}

Yes, you will need the getline function to read the next line.

    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.

        }

    }

please note the usage of file in the while clause, which is important. istream object could be evaluated to boolean which is equivalent to file.good(). The reason that you want to check the state is the second getline() function may reach the end the of file and throws an exception. You can also add the check after the second getline call and add a break if !file.good() .

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;
}

then the check won't be necessary.

You need to create a new bool flag variable that you set when u find a match, then loop again after finding your match so that you can get the next line. Test the flag to determine whether you've found a match in the previous loop or not.

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