This is a discussion on files & streams within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by gunghomiller Code: char ch=cin.get();; ... while (ch != EOF) a char cannot hold an EOF value. You'll ...
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...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...
If I have eight hours for cutting wood, I spend six sharpening my axe.