|
|
||||||
|
#1
|
|
|
|
|
I know this has probably come up frequently, but couldn't find a satisfactory reference... I have some code which needs
to read from stdin but must not block waiting for input if there is no input pending on stdin. I have some code which does the job (at least on my system... I wonder about the portability, though) but it's an ugly C hack; in particular I am not happy about mixing C and C++ style I/O. Simplified version: --- BEGIN CODE: test.cpp --- #include <cstdlib> #include <iostream> #include <sstream> bool inavail(FILE* stream) { long int nchars; if (fseek(stream,0,SEEK_END)!=0) return false; if ((nchars=ftell(stream))<0) return false; if (fseek(stream,0,SEEK_SET)!=0) return false; return (nchars>0); } using namespace std; int main() { ostringstream oss; if (inavail(stdin)) oss << cin.rdbuf(); cout << '[' << oss.str() << ']'; return EXIT_SUCCESS; } --- END CODE --- --- test.txt --- foo bar ------------------- "Correct" results: $ test < test.txt <RET> [foo bar] $ test <RET> [] (no user intervention required; if the call to inavail() is omitted, then the second run waits for user input and EOF) I guess I would be happy with a C++ equivalent of my inavail() function... the name is a giveaway: couldn't seem to get anything useful out of streambuf::in_avail(). Any hints much appreciated, |
|
|
|
#2
|
|
|
|
|
On Fri, 22 Apr 2005 11:21:50 +0100 in comp.lang.c++, "Lionel B"
<me> wrote, >I know this has probably come up frequently, but >couldn't find a satisfactory reference... That is because there is no satisfactory answer. |
|
#3
|
|
|
|
|
David Harmon wrote:
> On Fri, 22 Apr 2005 11:21:50 +0100 in comp.lang.c++, "Lionel B" > <me> wrote, > >>I know this has probably come up frequently, but >>couldn't find a satisfactory reference... >> That is because there is no satisfactory answer. Possibly not... nevertheless I would be happy to replicate the functionality of my (essentially C) inavail() function in a more C++ style - perhaps using streambuf. On the grounds of "Anything You Can Do In C You Can Do In C++ (tm)" this ought to be possible... |
|
|
| Similar Threads | |
| Best way to input from stdin? I'm writing a program that supports input from stdin. To be able to do that I tend to rely on a simple loop that tests the return of fgets(), such as the following... |
|
| Any way to take a word as input from stdin ? I searched the c.l.c archives provided by Google as Google Groups with "word input" as the key words and did not come up with anything good. C++ has std::string for taking... |
|
| Input using stdin How can I give input to a program using STDIN Suppose I want the program to take the value x=10 On some other site i found it as STDIN.read,but its not working. |
|
| getting input from stdin Hi Im new to unix scripting and now Im trying to get user input from stdin and this is what I did echo "enter your name: " read name and it will run with the pointer to... |
|
| Checking stdin for new data Has anyone been able to implement it on Windows in such way that the program can be started from a telnet session, not just from a command prompt in a native pseudo-DOS... |
|
|
All times are GMT. The time now is 03:44 AM. | Privacy Policy
|