Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Q: Reading from stdin without blocking?

Reply
Thread Tools

Q: Reading from stdin without blocking?

 
 
Jakob Bieling
Guest
Posts: n/a
 
      05-08-2005
Hi,

whenever I try to read input from stdin, the called function (ie.
std::cin.read ()) blocks until my buffer is full. But I want to function
to return immediately, regardless of having read something or not. Is
this possible? Here's the tiny code that blocks:

#include <iostream>

int main ()
{
char tmp [64];
std::cin.read (tmp, sizeof tmp);
}


Thank you
--
jb

(reply address in rot13, unscramble first)


 
Reply With Quote
 
 
 
 
Rolf Magnus
Guest
Posts: n/a
 
      05-08-2005
Jakob Bieling wrote:

> Hi,
>
> whenever I try to read input from stdin, the called function (ie.
> std::cin.read ()) blocks until my buffer is full. But I want to function
> to return immediately, regardless of having read something or not. Is
> this possible?


No. Not in Standard C++. What you can try is check if something is available
before reading, but it isn't guaranteed to work in all circumstances. It
might or might not on your platform. std::cin.rdbuf()->in_avail() should
(but doesn't need to) return the number of characters available for
reading.
If you want true non-blocking IO, you need to resort to system-specific
functionality.

> Here's the tiny code that blocks:
>
> #include <iostream>
>
> int main ()
> {
> char tmp [64];
> std::cin.read (tmp, sizeof tmp);
> }
>
>
> Thank you


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
peek at stdin, flush stdin Johnathan Doe C Programming 5 05-17-2013 04:30 PM
How to pass stdin of a C++ program to the stdin of a process createdwith ShellExecute() Ben C Programming 2 08-29-2009 09:47 PM
STDIN, OUT, ERR and $stdin, out, err - Differences? Terry Cooper Ruby 7 06-09-2009 05:48 AM
Reading from stdin then launching a program that reads from stdin strange behaviour Stefano Sabatini Perl Misc 6 07-29-2007 10:38 PM
Reading stdin once confuses second stdin read Charlie Zender C Programming 6 06-21-2004 01:39 PM



Advertisments