tags:

views:

705

answers:

4

I've got a program that is tailing a growing file.

I'm trying to avoid grabbing a partial line from the file (e.g. reading before the line is completely written by the other process.) I know it's happening in my code, so I'm trying to catch it specifically.

Is there a sane way to do this?

Here's what I'm trying:

if (getline (stream, logbuffer))
{
    if (stream.eof())
    {
        cout << "Partial line found!" << endl;
        return false;
    }
    return true;
}
return false;

However, I can't easily reproduce the problem so I'm not sure I'm detecting it with this code. std::getline strips off newlines, so I can't check the buffer for a trailing newline. My log message (above) is NEVER tripping.

Is there some other way of trying to check what I want to detect? Is there a way to know if the last line I read hit EOF without finding a EOL character?

Thanks.

+4  A: 

This will never be true:

if (getline (stream, logbuffer))
{
    if (stream.eof())
    {
       /// will never get here

If getline() worked, the stream cannot be in an eof state. The eof() and related state tests only work on the results of a previous read operation such as getline()- they do not predict what the next read will do.

As far as I know, there is no way of doing what you want. However, if the other process writes a line at a time, the problems you say you are experiencing should be very rare (non -existent in my experience), depending to some extent on the OS you are are using. I suspect the problem lies elsewhere, probably in your code. Tailing a file is a very common thing to do, and one does not normally need to resort to special code to do it.

However, should you find you do need to read partial lines, the basic algorithm is as follows:

forever do
   wait for file change
   read all possible input using read or readsome (not getline)
   chop input into lines and possible partial line
   process as required
end
anon
Not true. If the last line in the file does not end with newline, getline will succeed (failbit is not set) but the stream still goes to EOF state (because getline tried to read more characters). Even though eofbit is set, getline returns all that was read of the last line in logbuffer.
Tronic
A: 

Even if the other program isn't done writing the file, in the file that's where the line ends, so there's no way to tell the difference other than waiting to see if the other program writes something new.

edit: If you just want to tell if the line ends in a newline or not, you could write your own getline function that reads until it hits a newline but doesn't strip it.

Rusky
I guess what I'd like to discern is whether the last line read ended in a newline or not.
Joe
A: 

I have to take issue with one statement you made here:

However, I can't easily reproduce the problem so I'm not sure I'm detecting it with this code.

It seems like from what you said it would be extremely easy to replicate your problem, if it is what you said. You can easily create a text file in some text editor - just make sure that the last like ends in an EOF instead of going on to a new line. Then point your program at that file and see what results.

1800 INFORMATION
Hm. Good point.
Joe
That really does not reproduce the problem. It reproduces the OP's explanation of it, which may well be wrong.
anon
A: 

An istream object such as std::cin has a get function that stops reading when it gets to a newline without extracting it from the stream. You could then peek() or get() it to see if indeed it is a newline. The catch is that you have to know the maximum length of a line coming from the other application. Example (untested) code follows below:

char buf[81];  // assumes an 80-char line length + null char
memset(buf, 0, 81);

if (cin.get(buf, 81))
{
    if (cin.peek() == EOF)  // You ran out of data before hitting end of line
    {
        cout << "Partial line found!\n";
    }
}
Kristo