This is because iostream::eof
is set after reading the end of the stream (after the o occurs).
I'll explain what this means in the next example.
while(!inStream.eof()){
Accessing the //while statement means - not yet eof
inStream >> data;
// eof is now set -> data is not a valid value
/* Data Processing -> error */
}
On the other hand, in the following situations:
while(inStream >> data){
// The meaning of entering while means that read was successful (not eof)
// If it fails, operator >> returns false and fails to enter while
/* Data Processing -> ok */
}
It's safe because it is.
And for the second question,
if(scanf("...",...)!=EOF)
= = if(!(inStream >> data).eof())
!= != if(!inStream.eof()) inFile >> data
That's it. So
scanf("...",...)!=EOF
is a safe way to use.
© 2024 OneMinuteCode. All rights reserved.