Std::getline and eol vs eof?

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.

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.

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 Jan 9 '10 at 15:00.

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.

Hm. Good point. – Joe Aug 5 '09 at 20:15 That really does not reproduce the problem.It reproduces the OP's explanation of it, which may well be wrong.

– anon Aug 5 '09 at 20:22.

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.

I guess what I'd like to discern is whether the last line read ended in a newline or not. – Joe Aug 5 '09 at 20:17.

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 buf81; // 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.

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 cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions