Originally Posted by
jimblumberg
What makes you think it won't work with a single character?
Jim
Well the file reading logic is incorrect. Because EOF is a condition, not a character. After reading the first byte of a 1 byte file, EOF will be set to true and the main loop will not execute. Also, if the file is empty the file stream will enter a fail state and EOF should return true immediately with the same effect: the loop will not execute.. thus "eating" a character. It would be simpler to use
Code:
while (cin << input) { blah; blah; blah; }
in a C++ program.
I know because coding a reliable input processing system, even as simple as this, is not that straightforward unless you understand the error conditions that can occur while reading from the file.
EDIT:
Ok, say the input file is 1 byte. Then, after executing the code
Code:
char c;
cin << c; // or whatever wierd C++ thing to read a byte from a file LOL
The EOF condition will be set to true. And the main loop will not execute. Also, if the file is empty, the cin << inFile will set the input stream into a fail state, and should also set EOF, and the main loop will not execute either way.
I will note that the cin and cout operators << and >> are highly non-intuitive and confusing because, despite using them hundreds of times in practice, I still know that my usage here is probably incorrect ... due to its non-intuitive nature.
I mean really wouldn't it be
Code:
char c = '\0';
c << cin;
to read a byte?
Or is it
Or is there really any sense at all to it.. not really.. just learn the definitions I suppose. I myself would highly prefer something like
Code:
char c = fgetch(stdin);
which, I think, makes its pretty damn clear whats happening there...