When I run the code below, I use three inputs (in Ubuntu terminal):
- abc(Ctrl+D)(Ctrl+D)
 - abc(Ctrl+D)(Enter)(Ctrl+D)
 - abc(Enter)(Ctrl+D)
 
The code reacts well in all cases. My question is: why in 1) and 2) I need two EOF?
#include <iostream>
int main()
{
  int character;
  while((character=std::cin.get())!=EOF){}
  std::cout << std::endl << character << std::endl;
}
				
                        
That's how the "EOF" character works (in "canonical" mode input, which is the default). It's actually never sent to the application, so it would be more accurate to call it the
EOFsignal.The
EOFcharacter (normally Ctrl-D) causes the current line to be returned to the application program immediately. That's very similar to the behaviour of theEOLcharacter (Enter), but unlikeEOL, theEOFcharacter is not included in the line.If the
EOFcharacter is typed at the beginning of a line, then zero bytes are returned to the application program (since theEOFcharacter is not sent). But if areadsystem call returns 0 bytes, that is considered an end-of-file indication. So at the beginning of a line, anEOFwill be treated as terminating input; anywhere else, it will merely terminate the line and so you need two of them to terminate input.For more details, see the .Posix terminal interface specification.