1

I have a program that is supposed to get input from a user until it receives EOF.

\n or white spaces are considered as legal chars, but the console recognizes neither ^z nor ^d as EOF and the program continues to run until stopped manually.

Tried both:

while (currChar != EOF)
{
scanf("%c", &currChar);
}

and:

scanf("%c", &currChar);
if (currChar==EOF)
 break;
Ondrej K.
  • 8,841
  • 11
  • 24
  • 39
  • 1
    Relevant to the problem - [Why is `while ( !feof(file) )` is always wrong?](https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) – R Sahu Jun 11 '18 at 21:45
  • 1
    Also what is the type of `currChar` ? – Eugene Sh. Jun 11 '18 at 21:45
  • Thanks @user3121023, this fixed is – dasha tokarev Jun 11 '18 at 21:59
  • 2
    Something that needs to be emphasized is that in the context of C I/O routines, there's no end-of-file *character* like a newline or backspace or other control character. It's not something that's read from the stream. It's a *condition* that's set by the I/O routine when a read fails due to being at the end of the file - basically, a flag in the `FILE` object is set to indicate that there's no more input. The I/O routines return `EOF` as an *error code*. – John Bode Jun 11 '18 at 22:19
  • windows 10 is irrelevent to your problem, please remove that tag – phuclv Jun 12 '18 at 01:37
  • FYI, if ^Z is the first character of a line read from the console, `ReadFile` sets the number of bytes read to 0, but only if the console is in processed-input mode (enabled by default). Note that if you set `stdin` to Unicode text mode via `_setmode(_fileno(stdin), _O_U16TEXT)` and switch to using `wscanf`, then the console is read via `ReadConsoleW`, which does not implement this ^Z behavior. Instead it's the standard I/O in the Windows C runtime that looks for ^Z, just like when reading a disk file in text mode. But neither the console nor the Windows CRT implement ^D to terminate a read. – Eryk Sun Jun 12 '18 at 05:33

2 Answers2

4

scanf() doesn't set the variable to EOF when it gets to the end of the input, it returns EOF. So you have to test the value of the function.

while (scanf("%c", &currChar) != EOF) {
    ...
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • 1
    In general, if there are `n` conversions in the `scanf()`, the best test is `while (scanf("…", …) == n)` which detects EOF _and_ conversion failures. With a single `%c` conversion, the EOF test works because you'll either get 1 or EOF, but with a conversion such as `%d`, you can get `0` returned (because the next character that's not white space is also not a digit or a sign). – Jonathan Leffler Jun 11 '18 at 22:07
  • 1
    If you also want to check for conversion failures, you would ideally assign the return value to a variable and check for that separately. – Barmar Jun 11 '18 at 22:08
  • Yes. And if you want to reparse the input, then you use `fgets()` or POSIX `getline()` and parse with `sscanf()` instead of `scanf()`, so that if there's a problem with the input, you can either try again with a different format string or report the error more sanely. (The list goes on… :D) – Jonathan Leffler Jun 11 '18 at 22:13
0

Well, EOF is not a character. It's an integer constant with the value of -1. With that said if you enter with the value of EOF, your char variable would read only the - (minus) from -1 value. That's why the loop won't stop, they'll never be equal.

phuclv
  • 37,963
  • 15
  • 156
  • 475