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

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.


I have never programmed C in windows so I can't tell you but in bash, the program will get an EOF when you type end of data (Ctrl+D)


while ((c = read(0, buffer, BUFSIZ)) > 0) { 

You don't say the type of c but using that name implies that it's a char. Note that the EOF value for iosteams is an (int) -1. Storing that into an unsigned char will get you a value of 255 which will not match EOF.

Tags:

C++

C