c++ fastest way to read only last line of text file?

前端 未结 7 561
温柔的废话
温柔的废话 2020-11-27 20:35

I would like to read only the last line of a text file (I\'m on UNIX, can use Boost). All the methods I know require scanning through the entire file to get the last line wh

7条回答
  •  没有蜡笔的小新
    2020-11-27 21:10

    Use seekg to jump to the end of the file, then read back until you find the first newline. Below is some sample code off the top of my head using MSVC.

    #include 
    #include 
    #include 
    
    using namespace std;
    
    int main()
    {
        string filename = "test.txt";
        ifstream fin;
        fin.open(filename);
        if(fin.is_open()) {
            fin.seekg(-1,ios_base::end);                // go to one spot before the EOF
    
            bool keepLooping = true;
            while(keepLooping) {
                char ch;
                fin.get(ch);                            // Get current byte's data
    
                if((int)fin.tellg() <= 1) {             // If the data was at or before the 0th byte
                    fin.seekg(0);                       // The first line is the last line
                    keepLooping = false;                // So stop there
                }
                else if(ch == '\n') {                   // If the data was a newline
                    keepLooping = false;                // Stop at the current position.
                }
                else {                                  // If the data was neither a newline nor at the 0 byte
                    fin.seekg(-2,ios_base::cur);        // Move to the front of that data, then to the front of the data before it
                }
            }
    
            string lastLine;            
            getline(fin,lastLine);                      // Read the current line
            cout << "Result: " << lastLine << '\n';     // Display it
    
            fin.close();
        }
    
        return 0;
    }
    

    And below is a test file. It succeeds with empty, one-line, and multi-line data in the text file.

    This is the first line.
    Some stuff.
    Some stuff.
    Some stuff.
    This is the last line.
    

提交回复
热议问题