peek()-ing cin?

This is a discussion on peek()-ing cin? within the C++ Programming forums, part of the General Programming Boards category; Hi, I noticed that there is a peek() function in <fstream>. Is there an equivalent for cin? Actually, I just ...

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

    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
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,263
    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,183
    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, 12:14 PM
  2. problem with cin
    By markucd in forum C++ Programming
    Replies: 2
    Last Post: 04-16-2006, 01: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, 03:57 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21