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



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

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "Bob Smith"
Date: 16 Oct 2003 03:21:40 AM
Object: alternative to rdbuf()->in_avail()? [was is cin always hte keyboard's input]
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
.

User: "tom_usenet"

Title: Re: alternative to rdbuf()->in_avail()? [was is cin always hte keyboard's input] 16 Oct 2003 07:32:21 AM
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
.


  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER