[GD-General] OT: Checking if any chars available on stdin under Windows
Brought to you by:
vexxed72
From: Colin F. <cp...@ea...> - 2004-04-23 14:29:27
|
2004 April 22nd Friday Off-Topic question here...but I just spent an hour doing Google searching and reading other online documentation, and I'm not finding any answers to the following: QUESTION: How can I check if stdin (standard input) has any characters available for reading under WINDOWS (not Linux)? I thought (cin.rdbuf()->in_avail()) would be the cool hacked solution -- but even after doing Sleep(1000), cin.sync_with_stdio(), and wackier and wackier things to try to make "cin" up-to-date, so to speak, the expression above ALWAYS yields zero, whether or not there are characters available on the input stream. (Compiled using Visual C++ 6.0 under Windows 2000) If I have a command line like the following: testapp then the following code blocks indefinitely: int c = cin.peek(); but if the command line is as follows: more textfile.txt | testapp then the application does not block. But in both cases (cin.rdbuf()->in_avail()) is zero. How can "testapp" in the two command line examples above DETECT which scenario (piped input or not) is involved? That, I guess, is my real goal. I want to know if the command line lacks the piping from another app, since this means I shouldn't wait forever for stdin data. I'd be willing to use a Win32 function to solve this problem, but if it is really tricky, like creating a thread, or something nutty, then it's probably not worth it. Bleah, under Linux one can actually use fnctl() to change stdin to non-blocking mode (but, I assume, still buffered), which makes this problem go away -- but no fnctl() under Windows. Anyhow, probably avoiding piping data through stdin is the REAL solution to all of this mess, but that decision is not mine to make. --- Colin |