alternative to rdbuf()->in_avail()? [was is cin always hte keyboard's input]

Discussion in 'C++' started by Bob Smith, Oct 16, 2003.

  1. Bob Smith

    Bob Smith Guest

    so is there any alternative ( standard only ) to using in_avail()?
    I need to poll with given intervals the input stream, and if there is
    data to be read I want to read it.

    thank you
    /B
     
    Bob Smith, Oct 16, 2003
    #1
    1. Advertising

  2. Bob Smith

    tom_usenet Guest

    On Thu, 16 Oct 2003 11:21:40 +0300, Bob Smith
    <bobsmith@[dont-even-think-about-it]jippii.fi> wrote:

    >so is there any alternative ( standard only ) to using in_avail()?
    >I need to poll with given intervals the input stream, and if there is
    >data to be read I want to read it.


    There is no C++ standard alternative. There are POSIX standard and
    Win32 ways of doing it (O_NONBLOCK, select, WaitFor*), and no doubt
    other platforms have similar functionality.

    Note that if you use select, you should also use in_avail. After
    select has notified you of some waiting input, you should read a
    character, and then keep reading characters until in_avail is 0. e.g.

    //OS has said that characters are waiting on stdin (via select, say)

    std::vector<char> input;
    char c;
    cin.get(c); //this may cause a load of characters to be buffered
    input.push_back(c);
    //we now need to empty the buffer so that select will work next time
    while(cin.rdbuf()->in_avail() > 0){
    if (!cin.get(c))
    {
    //error
    }
    input.push_back(c);
    }

    //now ask OS again whether characters are waiting.



    If you use a non-blocking stream, then this is the way:

    // maybe there are characters waiting, maybe not

    std::vector<char> input;
    char c;
    while(cin.get(c))
    {
    input.push_back(c);
    }
    //no more waiting.

    if (cin.bad())
    {
    //error
    }
    //get cin ready for next time by clearing eof flag
    cin.clear();

    Tom
     
    tom_usenet, Oct 16, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. TaiwanNoWhere

    cin and cin.clear() problem

    TaiwanNoWhere, Oct 13, 2003, in forum: C++
    Replies:
    8
    Views:
    4,109
    P.J. Plauger
    Oct 17, 2003
  2. stephan beal

    is cin always the keyboard's input?

    stephan beal, Oct 15, 2003, in forum: C++
    Replies:
    11
    Views:
    656
    Bob Smith
    Oct 16, 2003
  3. Chris Mantoulidis

    std::cin.ignore() and std::cin.clear()

    Chris Mantoulidis, Jan 6, 2004, in forum: C++
    Replies:
    5
    Views:
    17,178
    Kevin Saff
    Jan 6, 2004
  4. berkay

    the function in_avail()

    berkay, Sep 27, 2005, in forum: C++
    Replies:
    1
    Views:
    339
    Jack Klein
    Sep 27, 2005
  5. Replies:
    2
    Views:
    1,398
Loading...

Share This Page