25

A question about this has been asked here End of File (EOF) in C but it still doesn't completely solve my problem.

EOF makes sense to me in any datastream which is not stdin, for example if I have some data.txt file, fgetc() will read all the chars and come to the end of file and return -1.

What I don't understand is the concept of EOF in stdin. If I use getchar(), it will wait for me to enter something, so if there is NOTHING written, End of File, (EOF) is not returned automatically?

So is it that only the user can invoke EOF in stdin by pressing Ctrl+Z? If so then what are some of the uses of EOF in stdin? I guess it tells the program to continue reading until the user invokes end of file? is this it?

Thank you

Community
  • 1
  • 1
Mcs
  • 534
  • 1
  • 5
  • 14
  • 4
    Yes only `ctrl+D` will give you EOF through stdin on unix. `ctrl+Z` on windows – Gopi Jan 29 '15 at 13:56
  • maybe it's a question about waiting for actual input or not and this may depend on [input redirection](http://stackoverflow.com/a/42831970/2932052) – Wolf Mar 16 '17 at 10:53
  • Relevant: https://devblogs.microsoft.com/oldnewthing/20110706-00/?p=10243 – Grault Jul 29 '22 at 03:42

4 Answers4

31

so if there is NOTHING written, End of File, (EOF) is not returned automatically?

No, it's not. It should be sent by the user.

So is it that only the user can invoke EOF in stdin by pressing Ctrl+Z?

Yes, you can set the EOF indicator for stdin with a special key combination you can input in the console, for linux console that is Ctrl+D and for windows it's Ctrl+Z.

If so then what are some of the uses of EOF in stdin? I guess it tells the program to continue reading until the user user invokes end of file? is this it?

The use of it depends on whether you instruct the user to input the EOF explicitly or not, for example, I think python console will tell you something like Press Ctrl+D or type quit() to exit.

And EOF is not necessarily -1 it's a macro and you should always use it to test for the EOF indicator. And more importantly EOF is not a character, it's a special value that indicates that the End Of File indicator is set.

Also, getchar() is equivalent to fgetc(stdin).

Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
  • thanks yeah that is what I was thinking too, also EOF might have uses then in emptying the input buffer? for example you can see if there is an EOF left in the buffer? It is slowly making sense – Mcs Jan 29 '15 at 14:02
  • I don't think so, because `EOF` is not a character. – Iharob Al Asimi Jan 29 '15 at 14:03
  • I will try it and see if it is left in the buffer – Mcs Jan 29 '15 at 14:04
  • 1
    "you can insert EOF in stdin" mis-leads a bit. Special key sequence like ^D signal to `stdin` that no more data will be appended. I think these answer needs greater emphasis of your comment "`EOF` is not a character" – chux - Reinstate Monica Jan 29 '15 at 14:56
9

In linux bash, if you press CTRL+D, it will generate EOF.

In Windows, the equivalent is CTRL+Z

So, no, if nothing written to the terminal, that does not generate EOF automatically. The scanning function is in wait state then. So, without having any other inputs, in wait state, if CTRL+D is pressed, the key press is translated [by the terminal driver] to EOF.Note

Usually, once you key in some value and press the ENTER key, the scannning function starts scanning. To feed an input for producing EOF, you need to press CTRL+D.

Related: Please reaed the wiki entry for EOF

Note: With thanks to Mr Drew for the clarification.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
2

stdin is a stream, data is not available until the user presses some keys. A file on the disk already has (a fixed amount of) content.

When reading from stdin, if getchar() doesn't wait for the user to input something then the program will always get EOF. That will make it impossible to use stdin as an input file.

Because getchar() waits for the user to input something there is no way to signal the input completed; that's why the operating systems provide a combination of keys that have this special meaning when they are pressed on the console.

Windows uses CtrlZ and Unix-like OSes (including OSX) use CtrlD for this purpose.

axiac
  • 68,258
  • 9
  • 99
  • 134
0

The file stdin is not always the user typing on the keyboard. If you redirect input to your program, it can be just a normal file.

program.exe <input-from-file.txt

What may be confusing you is that no giving input into a console window does not mark the end of the input. But think it the other way round: how could a user respond so quickly that the program would not terminate before it if the console would not do some buffering for the user? After pressing Enter the user says this is a line of input. In other words: a program running in a console window always waits for the next input to come.

Most programs define a special phrase to end a console session. You probably know exit.

Wolf
  • 9,679
  • 7
  • 62
  • 108