Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: C++
Expertise: All
May 18, 1998



Building the Right Environment to Support AI, Machine Learning and Deep Learning

Be careful with EOF

The standard library of the C programming language offers a function getc to read a single character from a file or stdin:
     char c = getc(stdin);
This function is useful if you really want to process all characters, because unlike scanf, it does not skip spaces, line breaks, tabs, unprintable characters, and the like. This function is also incorporated in the C++ standard:
     c = cin.get();  // A substitute for "c = getc(stdin);"
To process a file, you need a loop which terminates when the end of the file is seen. How do you formulate the break condition? You might know that an end-of-file indicator, EOF, is defined in stdio.h. So, do you think the following loop is correct?
     while (true)
        char c = cin.get();
        if ( c == EOF ) break;
        // Otherwise, process c.
Nope. If you switched on all compiler warnings, the line in which cin.get() was invoked might cause a warning. The reason is that cin.get() returns an int, not a char, and assigning an int value to a char variable is not a safe operation. In particular, the test for equality is not safe, because EOF is also an int, and its value does not fit into a char!

Usually, EOF==(int)-1, which on most machines means that all bits are ones. Hence, the test c==EOF fails even if EOF was returned by cin.get(), because the leading ones of EOF were truncated in the assignment to c. In the if-statement, c is converted into an int, but this means that the leading bits are set to zero.

You are always on the safe side when you forget about a C feature such as EOF and use the substituting C++ feature:

     if ( cin.eof() ) break;
Karsten Weihe
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date