Hi,
I noticed that there is a peek() function in <fstream>. Is there an equivalent for cin?
Actually, I just need a function that returns a bool - whether there is anything waiting in cin or not.
Thanks.
Hi,
I noticed that there is a peek() function in <fstream>. Is there an equivalent for cin?
Actually, I just need a function that returns a bool - whether there is anything waiting in cin or not.
Thanks.
peek() is a method of std::istream -- therefore, any istream should support it, including std::cin.
However, this will not do what you want. peek() will wait until a byte is available, then return it without consuming it. You want an early return if no byte is available. This is a highly non-portable operation.
Thanks for the reply.
Ah, didn't see that it was inherited from istream.
I see. I was under the impression that peek() is non-blocking. I find the name "peek" misleading to say the least.However, this will not do what you want. peek() will wait until a byte is available, then return it without consuming it. You want an early return if no byte is available. This is a highly non-portable operation.
Guess I will have to turn to boost::thread for asynchronous IO... a bit of an overkill I guess.