Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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   log, logFile("test.log");
size_t   p = 0;

while(true)
{
    ifstream ifs(logFile.c_str());

    ifs.seekg(p);  //*1

    while(ifs.eof() == false)
    {
        getline(ifs, log);

        cout << log << endl;

        p = ifs.tellg();  //*2
    }

    nanosleep(&pause, NULL);
}

Without the lines //*1 and //*2, the log file is correctly read up to its end, but if new lines are added nothing happens.

With seekg and tellg I am trying to store the current end position of the file, so that when I reopen it I can go strait there and read what has been added.

I would like to know what is wrong in my code, and if it is really necessary to close and reopen the same file for this purpose.

Thank you.

like image 714
Pietro Avatar asked Aug 01 '12 10:08

Pietro


People also ask

How to read stuff from a file in C?

Steps To Read A File:Open a file using the function fopen() and store the reference of the file in a FILE pointer. Read contents of the file using any of these functions fgetc(), fgets(), fscanf(), or fread(). File close the file using the function fclose().

How do I read from a file in C++?

File Handling in C++Create a stream object. Connect it to a file on disk. Read the file's contents into our stream object. Close the file.


2 Answers

Since none of these answers worked, i came up with one that does work...

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

using namespace std;

int main()
{
    string   log, logFile("test.txt");
    std::streamoff   p = 0;
    ifstream ifs(logFile.c_str());

    while(true)
    {

        ifs.seekg(p);  //*1
        while (getline(ifs, log))
        {
            cout << log << endl;
            if(ifs.tellg() == -1) p = p + log.size();
            else p = ifs.tellg();
        }
        ifs.clear();

    }
}
like image 189
Nick Kreissler Avatar answered Oct 08 '22 18:10

Nick Kreissler


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;
}
like image 36
hmjd Avatar answered Oct 08 '22 19:10

hmjd