Thread: peek()-ing cin?

  1. #1
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229

    peek()-ing cin?

    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.

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by cyberfish View Post
    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.

  3. #3
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    Thanks for the reply.

    Ah, didn't see that it was inherited from istream.

    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.
    I see. I was under the impression that peek() is non-blocking. I find the name "peek" misleading to say the least.

    Guess I will have to turn to boost::thread for asynchronous IO... a bit of an overkill I guess.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. cin problem
    By mikahell in forum C++ Programming
    Replies: 12
    Last Post: 08-22-2006, 11:14 AM
  2. problem with cin
    By markucd in forum C++ Programming
    Replies: 2
    Last Post: 04-16-2006, 12:50 PM
  3. getline(function)??
    By dac in forum C++ Programming
    Replies: 7
    Last Post: 03-10-2006, 12:42 PM
  4. cin not allowing input after first use of function
    By Peter5897 in forum C++ Programming
    Replies: 5
    Last Post: 01-31-2006, 06:29 PM
  5. Overriding Cin with Cout
    By Tainted in forum C++ Programming
    Replies: 5
    Last Post: 10-06-2005, 02:57 PM