ifstream::get() surprise
From: Jacek Dziedzic (jacek__NOSPAM___at_janowo.net)
Date: 08/26/04
- Next message: Jacek Dziedzic: "Re: reading files"
- Previous message: Phlip: "Re: A religious question: int* i; /*or*/ int *i;?"
- Next in thread: Victor Bazarov: "Re: ifstream::get() surprise"
- Reply: Victor Bazarov: "Re: ifstream::get() surprise"
- Reply: Mike Wahler: "Re: ifstream::get() surprise"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Thu, 26 Aug 2004 18:21:45 +0200
Hi!
Consider the following program
#include <fstream>
#include <iostream>
using namespace std;
int main() {
ifstream in("test.txt");
char buf[40];
in.get(buf,40);
cerr << "Read: *" << buf << "*, trouble: " << !in << endl;
}
and a file, test.txt, starting with an empty line, ie. a lone EOL
character on the first line.
I was quite surprised to find out, that under these circumstances
the aforementioned program produced
"Read **, trouble: 1".
Why does 'in' go to a fail state? I thought 'get' reads up to
the terminator, stores all characters into 'buf' and leaves the
terminator inside the stream. That would mean 'buf' containing
just a \0 char (no chars read), the EOL still in the stream, but
why a failed state? There are more lines in the file, so we're
not eof(), and my understanding of this situation is a
"successful read of zero characters" rather than "read error".
How then can I distinguish a successfull reading of an empty
line from an I/O error during reading a line? Of course I have
no a priori knowledge if these empty lines exist in my parsed
file or not.
TIA,
- J.
- Next message: Jacek Dziedzic: "Re: reading files"
- Previous message: Phlip: "Re: A religious question: int* i; /*or*/ int *i;?"
- Next in thread: Victor Bazarov: "Re: ifstream::get() surprise"
- Reply: Victor Bazarov: "Re: ifstream::get() surprise"
- Reply: Mike Wahler: "Re: ifstream::get() surprise"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|