How to read a growing text file in C++?

前端 未结 3 504
臣服心动
臣服心动 2020-11-29 06:26

I am trying to read from a file which is growing (something similar to what tail -F does), but there must be some problems with my code:

string          


        
相关标签:
3条回答
  • 2020-11-29 06:45

    The loop is incorrect as when eof() is encountered tellg() returns -1 and there is no check for eof() immediately after the call to getline() which there needs to be. Change loop to:

    while (getline(ifs, log))
    {
        cout << log << endl;
        p = ifs.tellg();
    }
    

    Additionally, as p is declared as a size_t when tellg() return -1 the value of p was being set to4294967295. This meant the seekg() was being set to beyond the end of the file. Change the type of p to std::streamoff and confirm the call to seekg() was successful:

    if (ifs.seekg(p))
    {
        while (getline(ifs, log))
        {
            cout << log << endl;
            p = ifs.tellg();
        }
    }
    

    if it is really necessary to close and reopen the same file for this purpose.

    No, it is not necessary but you need to clear() the eof state from the stream. The following is an alternative to a corrected version of the posted code:

    #include <iostream>
    #include <string>
    #include <fstream>
    
    int main()
    {
        std::ifstream ifs("test.log");
    
        if (ifs.is_open())
        {
            std::string line;
            while (true)
            {
                while (std::getline(ifs, line)) std::cout << line << "\n";
                if (!ifs.eof()) break; // Ensure end of read was EOF.
                ifs.clear();
    
                // You may want a sleep in here to avoid
                // being a CPU hog.
            }
        }
    
        return 0;
    }
    
    0 讨论(0)
  • 2020-11-29 06:48

    This method has worked faithfully for me:

    #include <string>
    #include <chrono>
    #include <thread>
    #include <fstream>
    #include <iostream>
    
    int main(int, char* argv[])
    {
        // open file passed in on command line (at end of file)
        std::ifstream ifs(argv[1], std::ios::ate);
    
        if(!ifs.is_open())
        {
            std::cerr << "ERROR: opening log file: " << argv[1] << '\n';
            return 1;
        }
    
        // remember file position
        std::ios::streampos gpos = ifs.tellg();
    
        std::string line;
        bool done = false;
    
        while(!done)
        {
            // try to read line
            if(!std::getline(ifs, line) || ifs.eof())
            {
                // if we fail, clear stream, return to beginning of line
                ifs.clear();
                ifs.seekg(gpos);
    
                // and wait to try again
                std::this_thread::sleep_for(std::chrono::milliseconds(100));
                continue;
            }
    
            // remember the position of the next line in case
            // the next read fails
            gpos = ifs.tellg();
    
            // process line here
            std::cout << "line: " << line << std::endl;
        }
    }
    
    0 讨论(0)
  • 2020-11-29 06:55

    This code works for me:

    struct timespec pause;
    pause.tv_sec  = 1;
    pause.tv_nsec = 0;
    
    std::ifstream ifs("test.log");
    std::streamoff p;
    
    if(ifs.is_open())
    {
        std::string line;
    
        while(true)
        {
            if(ifs.seekg(p))
            {
                while(std::getline(ifs, line))
                {
                    std::cout << line << std::endl;
                    p = ifs.tellg();
                }
            }
    
            ifs.clear();
    
            nanosleep(&pause, NULL);
        }
    }
    
    0 讨论(0)
提交回复
热议问题