Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I force eof on stdin?

Tags:

c++

stdin

eof

For a C++ application, how can I programmatically force an end of file (EOF) on stdin?

like image 791
WilliamKF Avatar asked Jan 15 '11 20:01

WilliamKF


People also ask

How do you do EOF on the keyboard?

In Windows, Control+Z is the typical keyboard shortcut to mean "end of file", in Linux and Unix it's typically Control+D .

How does bash handle EOF?

This operator stands for the end of the file. This means that wherever a compiler or an interpreter encounters this operator, it will receive an indication that the file it was reading has ended. Similarly, in bash, the EOF operator is used to specify the end of the file.

What is considered EOF?

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.


1 Answers

If you're at a terminal in a Unix-like system, hit Ctrl-D. In Windows, Ctrl-Z.

Edit: Having seen the desire to do this "programmatically," I suggest trying fclose(stdin). If somehow that's not good enough, a crazier idea might be to use std::cin.rdbuf() to set the stream to refer to something other than the true stdin, and then do what you want to that stream. But this starts to smell bad, like you are fighting against the computer, so I'd like to know more about what the real goal is.

like image 129
John Zwinck Avatar answered Sep 19 '22 13:09

John Zwinck