#1
  1. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2002
    Location
    Seattle WA
    Posts
    863
    Rep Power
    14

    How to tell if there's unread info in an istream?


    I have a function that parses a command line. Problem is though, I need it to do one thing if supplied with some command line arguments, and another if it isn't. But I'm not sure how to probe the stream to see if there's any data in it. For example,
    Code:
    // Forward declaration.
    void cmd_help(istream &);
    
    // Parse the command line.
    std::string cmd;
    cin >> cmd;
    if (cmd == "help")
      cmd_help(cin);
    else if
      // etc...
    
    void cmd_help(istream & in)
    {
      // If there's remaining info in the stream, interpret it.
      if (in.???)
      {
        std::string info;
        in >> info;
        // Interpret the info string.
        // ...
      }
      else
        // Just display the default help message.
        cout << ST_HELP;
    }
    I could always just read the entire line at the beginning using getline(), then parse the string myself, but I'm trying to put this in a program I already wrote, and I'd rather not make any changes to the command line implementation.
  2. #2
  3. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    243
    I am far from a C++ IO guru, but isn't there a 'peek' function? If all you want to do is know if the stream is there or not, simply opening it and looking for EOF should tell you that, shouldn't it?

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2002
    Location
    Seattle WA
    Posts
    863
    Rep Power
    14
    Yeah, there is. Been there done that. Got it working anyways though...

    Funny...I got so far as (unsuccessfully) playing with basic_streambuf::sgetc() which is called by peek(). The problem though, is I was testing for the wrong character. You need to test for the trailing newline char, NOT an EOF bit...

    Turns out the cin methods flush the buffer _before_ a call, not after. Makes sense I suppose. Sometimes it's the simplest things. Well, usually it's the simplest things...

    Code:
    if (cin.peek() != '\n')
      // Remaining data in the buffer.
    Last edited by MJEggertson; September 2nd, 2003 at 02:30 AM.

IMN logo majestic logo threadwatch logo seochat tools logo