1

Soo... I saw a guy claim this code was working on another question.

while(scanf("%X", &hex) != EOF) {
    //perform a task with the hex value. 
}

So, in what context does the EOF flag get thrown? I though it would just keep asking for a number indefinitely. I added another line of code to test it, and it does exactly what I expected it too.....

This isn't a file, this seems to be stdin. So.... WHEN is this code useful?
Ie, in what context is the EOF return thrown?

user1833028
  • 865
  • 1
  • 9
  • 18
  • 1
    I think if you press ctrl-D you may get the EOF... – Floris Feb 15 '13 at 04:22
  • 1
    See [this earlier question](http://stackoverflow.com/questions/11944314/ctrl-d-didnt-stop-the-whilegetchar-eof-loop) – Floris Feb 15 '13 at 04:23
  • `EOF` isn't a flag that's thrown. It's a macro that expands to a constant expression of type `int`, typically `(-1)`. `scanf` returns the value of `EOF` when it's defined to do so by the standard. – Keith Thompson Feb 15 '13 at 05:57

3 Answers3

1

If you look at the documentation for scanf, you will read that the value EOF is returned if a read failure occurred before the first value was assigned. (ie end of file)

http://en.cppreference.com/w/cpp/io/c/fscanf

You could equally test:

while(scanf("%X", &hex) == 1)

This is my preference. I expect one input, so I will be explicit.

paddy
  • 60,864
  • 6
  • 61
  • 103
  • And what sir... would be likely to cause such a read failure? – user1833028 Feb 15 '13 at 04:26
  • That particular failure would occur if the input stream is closed (`EOF`). If the input is simply invalid (*ie* not a hexadecimal character), it will return `0` instead. – paddy Feb 15 '13 at 04:34
  • What this means is that if you loop while not `EOF` and you receive bad input, as it currently stands you won't realise it and you will keep looping indefinitely. Consider storing the result of the function call in a variable or using my more explicit loop condition. – paddy Feb 15 '13 at 04:37
  • Thanks... between us, I think we've put together a whole answer! – user1833028 Feb 15 '13 at 04:37
0

Realistically speaking, this input is good on linux because ^d will end the stream, thus throwing the 'error.'

On windows, this behavior is different... whatever it is is not ctrl+d. At least I know now though, since I use both.

Thanks!

user1833028
  • 865
  • 1
  • 9
  • 18
  • 1
    On DOS (and early Windows, haven't checked in a _long_ time) end-of-file was signalled with ctrl-Z. – vonbrand Feb 15 '13 at 04:44
0

EOF is returned on I/O error and end-of-file. With stdin, an I/O error is a rare event and with keyboard input the end-of-file indication usual takes a special key sequence.

A practical use occurs with redirected input.

Assume a program exists that reads hexadecimal text and prints out decimal text:

// hex2dec.c
#include <stdio.h>    
int main(void) {
  unsigned hex;
  int cnt;
  while((cnt = scanf("%X", &hex)) == 1) {
    printf("%u\n", hex);
  }
  // At this point, `cnt` should be 0 or EOF
  if (cnt != EOF) {
    puts("Invalid hexadecimal sequence found.");
    return 1;
  }
  return 0;
}

// hex.txt contents:
abc
123

Conversion occurs with the command

hex2dec < hex.txt
2748
291

By detecting EOF on the stdin, the program knows when to return.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256