473,320 Members | 1,853 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Checking available input on stdin

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,

--
Lionel B

Jul 23 '05 #1
2 3141
On Fri, 22 Apr 2005 11:21:50 +0100 in comp.lang.c++, "Lionel B"
<me@privacy.net> wrote,
I know this has probably come up frequently, but
couldn't find a satisfactory reference...
That is because there is no satisfactory answer.
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.


Jul 23 '05 #2
David Harmon wrote:
On Fri, 22 Apr 2005 11:21:50 +0100 in comp.lang.c++, "Lionel B"
<me@privacy.net> 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...

--
Lionel B
Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Tongu? Yumruk | last post by:
I have a little proposal about type checking in python. I'll be glad if you read and comment on it. Sorry for my bad english (I'm not a native English speaker) A Little Stricter Typing in Python...
1
by: Scott Shaw | last post by:
Hi all, I was wondering if you could help out with this problem that I am having. What I am trying to do is detect keyboard input in a while loop without halting/pausing the loop until the key is...
9
by: kernelxu | last post by:
hi,everybody. I calling function setbuf() to change the characteristic of standsrd input buffer. some fragment of the progrem is: (DEV-C++2.9.9.2) #include <stdio.h> #include <stdlib.h> int...
4
by: veydel | last post by:
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 window? I tried all kinds of...
8
by: Brendan | last post by:
There must be an easy way to do this: For classes that contain very simple data tables, I like to do something like this: class Things(Object): def __init__(self, x, y, z): #assert that x,...
8
by: GiBo | last post by:
Hi! Classic situation - I have to process an input stream of unknown length until a I reach its end (EOF, End Of File). How do I check for EOF? The input stream can be anything from opened file...
6
by: AZRebelCowgirl73 | last post by:
Here is my problem: I have two java files: One named Car.java and the other named CarDealerApp.java: In the CarDealerApp program, I read in through user input the make, model, year and price of...
125
by: jacob navia | last post by:
We hear very often in this discussion group that bounds checking, or safety tests are too expensive to be used in C. Several researchers of UCSD have published an interesting paper about this...
7
by: CaptainnFungi | last post by:
Hi All, I am very new to C and have been working my way through a few C books with the aim of getting more knowledge in programming. However I have hit a wall and I am not sure how to get over it....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.