Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

End of File(EOF) of Standard input stream (stdin)

Tags:

c++

c

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?

like image 279
Ravi Gupta Avatar asked Jul 07 '10 16:07

Ravi Gupta


People also ask

How do I end an EOF file?

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.

How do you stop a stdin input?

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.

What is EOF in character input stream?

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?)

How do you indicate EOF in stdin?

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 .


1 Answers

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.


Edit

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.

like image 174
Archie Avatar answered Sep 22 '22 08:09

Archie