简体   繁体   中英

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

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?

Speaking about EOF in stdin: when you redirect input from file, eg:

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 .

我从来没有在 Windows 中编写过 C,所以我不能告诉你,但是在 bash 中,当你输入数据结束时,程序会得到一个 EOF (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.

First getchar() is really getc(stdin) so getc(FILE) you can read more from that. getchar() gets you last unprocessed character from input stream or stdin pressing enter is '\\n'. if the stdin is empty getchar forces a pause to get input. Say in a program I call getchar() at first the stdin is empty so it pauses for input. If I enter ab'\\n' the first getchar() will get 97 the ascii of 'a'. the next time i call getchar() i will get b , then again getchar() will have '\\n'.

To prove this write this code.

    int choice;

    do
   {
      cout << "Enter input:" ;
      choice = getchar(); 
      cout << "Last getchar(): " << choice << ":" << (char) choice ; 
       if( choice == '0' || choice == EOF)
         {
           cout << "Exited loop" << endl;     // EOF is ctrl+z in windows 
           break;
         }

   }while(true);

I do believe stdin is a global so until getchar() or similar function gets called it to clear the stream the characters remain there which can cause bugs later if you use getchar() elsewhere. As people have mentioned you can use gets(char[]) which puts all characters until newline into the character array. The problem with this is you need a char[] larger than input or you will get errors. The nice thing is gets(char[]) does clear stdin so you can make a dumby buffer to clear stdin or process it.

I hope this is informative.

The way to test for EOF is to check the return value of fread, and then use feof:

while( fread( ... ) ) {    // loop so long as fread does not return zero
    // do something
}

if ( feof( stdin ) ) {
   // read failed because of EOF
}
else {
   // some other error
}

Your solution is tagged C++, so here's some C++.

std::string lols;
while(!(std::cin >> lols).eof()) { // This loop will end when EOF is reached
    // Process string
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM