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 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.
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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....
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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...
|
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...
|
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)...
|
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...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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....
| |