Currently I have a program that reads from the standard input, occasionally the program needs to just keep running if no input is made, usually this is a test script there is no 'enter' so to speak.
program -v1 -v2 -v3 <input >output
v1 - v3 are command line arguments respectively
Basically the program spits out the command line arguments and their respective meaning to the program if no 'input' is given and then should exit.
However at the moment if give it an empty test file or just run without pressing enter after running it blocks on the std::getline I use to input the commands.
while(std::getline(std::cin,foo)
{do stuff}
where foo is a string.
How do I get it to just run through and do stuff
at least once then exit in the event of no input? In the event of input the do stuff
occurs once for every line in standard input.
Would a switch to a do-while loop, with a check pre loop as to whether it's got any input, work?
Something like
if cin empty
set flag
do
{do stuff
check flag}
while(getline)
or is non-blocking io not possible in c++?
This question seems to be rehashed over and over but I couldn't find a definitive answer or even an answer that was platform agnostic(this program is academic in nature, coded on windows and tested on unix).
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…