J
Jacek Dziedzic
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.
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.