Ooops, forgot the sample
BOOL stdin_ready() {
DWORD dwNumEvents;
GetNumberOfConsoleInputEve
if ( dwNumEvents) return TRUE;
return FALSE;
};
Main Topics
Browse All TopicsI have been trying, without success, to find some function on Windows that will simply tell me if a call to getc(stdin) will not block (i.e. there is one or more characters waiting on stdin.) This is very simple on *nix systems. Also, the polling solution should be able to work regardless of whether the program is running in a Win32 console or was started by another process that uses stdio to communicate to it.
The main loop of my program is best represented by this pseudo-code:
for (;;) {
while (stdin_ready()) {
read_one_char_from_stdin()
do_something_with_char();
}
while (socket_ready())
handle_socket();
}
On both platforms, the socket_ready() code uses select() to poll the socket. On *nix, stdin_ready() also uses select(), but I cannot do this on Windows, as select() is provided by the Winsock library, so it only works on sockets.
A solution may use isatty() if it is 100% necessary to split code based upon whether the program is being run in a console. However, I will prefer solutions that "do not care" about where the program is run. Basically, I'm looking for a solution that uses stdio in the purest form.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
There is a function getc_unlocked() defined in stdio.h and it conforms to POSIX.1 ... I am not sure if windows has it since windows does not conform to POSIX ... still give it a try ... If they do not have getc_unlocked(), you may find something equivalent like _getc_unlocked() in their header files
Okay, I have tested on my brother's XP box. I was not able to get GetNumberOfConsoleInputEve
I am using select() for the network, and I am not sure if that is even working correctly. Maybe a broader question is what is the "standard" way to write a console app for windows that needs to juggle stdin and a socket? The program is a "telnet helper" -- a program calls it in place of opening a telnet connection, and uses its stdio as if it were coming from the server. This means that my program has to handle the socket instead, but means that I get to do all sorts of interesting manipulations and change the data going through.
So... if you know of "the right way" to do something like that on Windows, please advise.
Business Accounts
Answer for Membership
by: jkrPosted on 2005-03-09 at 12:37:28ID: 13500159
On Windows, you'd either use 'PeekConsoleInput()' or 'GetNumberOfConsoleInputEv ents()' to achieve that.