Jump to content
  • Advertisement
Sign in to follow this  
EvilCrap

test cin for characters -non block

This topic is 4828 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I need to test std::cin to see if its ready to be read, without blocking, how do I do this? cin.peek doesnt work, as it blocks. for example:
        if( stream_has_chars ){
            char c=0;
            c =std::cin.get();
            if( c == 'q' ){
                break;
            }
        }

Is there a bit of code that works as stream_has_chars? cin is being sent data by another program writting to the stream.

Share this post


Link to post
Share on other sites
Advertisement
Are you worrying about cin stalling because other programs are using it? If so, there really is no need. cin is only really used on console programs that are user operated, and I think it may even be application specific. Don't hold me on that though. If there is a stall, you will not notice it unless you are an android from 200 years in the future. cin also stalls for user input, so any stall would be irrelevant as it would have passed by the time the user has pressed the enter key. If you are worried about any trash data, just flush the stream.

Share this post


Link to post
Share on other sites
No, I have Java program and a C++ program. The Java program writes to the C++ program's cin, and reads the cout and cerr. I am able to read the Java input if i do cin, get, or peek, but these block, and I want non-block.

For some reason, readsome returns zero all the time, and in_avail() also returns zero, while cin and such do not.

I am looking for a way to poll std::cin and not block.

Share this post


Link to post
Share on other sites
Maybe something like end of file ?

http://www.cplusplus.com/ref/iostream/ios/eof.html


BTW why do you want to let the console app do busy waiting ? If there is no work to do, the application should block to allow other programs to use the cpu.
Another option is to use threads, so that the input thread can block, while the worker thread performs its job. This will make your application design easier. Usually a synchronisation is needed when the input is transferred to the worker thread.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!