The problem form http://acm.uva.es/p/v102/10252.html
How to terminate it when EOF or pressing CRTL-D?
Printable View
The problem form http://acm.uva.es/p/v102/10252.html
How to terminate it when EOF or pressing CRTL-D?
If you think about it, what you enter can't be -1, because ``-1'' is two characters, and getchar is reading one character at a time. It turns out that the value of EOF as seen within your C program has essentially nothing to do with the keystroke combination you might use to signal end-of-file from the keyboard. EOF is essentially a signal to your program that no more characters will be available from that input, for whatever reason (end of a disk file, user is done typing, network stream has closed, I/O error, etc.).
Depending on your operating system, you indicate end-of-file from the keyboard using various keystroke combinations, usually either control-D or control-Z. The operating system and stdio library then arrange that your C program receive the EOF value. (Note, however, that there are various translations involved along the way. Under normal circumstances, you should not explicitly check for the control-D or control-Z value yourself, nor will you find that the <stdio.h> macro EOF is defined to be either of these values.)
(source [link] http://c-faq.com/stdio/eofval.html [/link )
thank KoG.
If I want to do this
How do I read it?Code:input
-------------------
This is testing text.
I will press CTRL-D to end of input. [CTRL-D]
Ctrl+D sends EOF to the program. so test for EOF
while ( fgets( buff, sizeof buff, stdin ) != NULL )
This exits when you enter the EOF sequence.
As does this
while ( (ch=getchar()) != EOF )
ctrl-d is normally defined by stty program on login as meaning EOF - so the effect of particular keystroke(s) is defined on a per-user level.
A lot of Linux/UNIX command line utilities honor EOF - ones like bc for example.
Anyway, unless you are guaranteed that ctrl-d does generate EOF, don't put something in your menu that says "enter ctrl-D to exit".