a char cannot hold an EOF value. You'll need an int for that.
Also, you use the ch value before you check to see if it's not EOF.
also, try turning your warning levels up. You have a useless semicolon there.
Printable View
As far as i'm aware, EOF is #define'd as -1 - so a char is perfectly well equipped to hold it.
That matter aside, reading from a file while-not-EOF is the wrong idiom, the correct way is to read while there is data to read. in other words,This ensures that the state of the ifstream is checked at the same time as reading. if anything goes wrong (like attempting to read past the end of the file), the loop stops before anything bad can happen.Code:std::ifstream infile("test.txt");
char ch;
while( infile >> ch )
{
// do something with ch
}
Char may be unsigned and "equipped" to hold values from 0 to 255...Quote:
As far as i'm aware, EOF is #define'd as -1 - so a char is perfectly well equipped to hold it.
When you store the EOF in char - you get something like 255 - which is fully legal value for the char...
When you get the file containing this value - you will stop reading it on this char just because you do not want to hear when people knowing something better telling you how you should do this or that...