Does stdin have any EOF? For example, if I start reading from stdin using fread
or read
, then will the following loop end?
while ((c = read(0, buffer, BUFSIZ)) > 0) {
.
.
.
}
If the answer to this question is no, then is there any way to add EOF to stdin?
The End of the File (EOF) indicates the end of input. After we enter the text, if we press ctrl+Z, the text terminates i.e. it indicates the file reached end nothing to read.
press CTRL-D to end your input. This is the right answer. Ctrl-D is the canonical way to terminate keyboard stdin in any shell command.
A useful member function of the input stream classes is eof() stands for end of file. returns a bool value, answering the question "Are we at the end of the file?" (or is the "end-of-file" character the next one on the stream?)
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 .
Speaking about EOF
in stdin: when you redirect input from file, e.g.:
program <input.txt
the file already has an EOF
, so this is not a problem. In console you can simulate EOF
flag. In UNIX systems it is Ctrl+D, in Windows Ctrl+Z. When you type this in the console, program will behave like it has just reached end of input file.
According to a question asked by OP:
So does it means that stdin don't have EOF and we have to insert them manually using Ctrl+Z or Ctrl+D?
Actually -- yes. One may consider stdin (not redirected, but taken from the console) as infinite file -- no one can tell where does it end. The end of input file, where input ist stdin, must be told literally by Ctrl+D or Ctrl+Z.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With