473,406 Members | 2,377 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,406 software developers and data experts.

end of stream for std::cin

What is cin's streambuffer's state and content after the following
(using namespace std)?

string((istreambuf_iterator<char>(cin)),
istreambuf_iterator<char>()) );

I am trying to use this in a loop (to get a 'multiple cat'-like
behavior) and the second time around it returns an empty string
instead of waiting for more input. This makes me think that either the
end-of-stream stays in the buffer or cin or its buffer is in some bad
state. Either way, how can this be fixed to get more input?

Thanks,
S.
Feb 22 '08 #1
7 5077
On Feb 22, 7:20 am, Sanyi <Sandor.Benc...@gmail.comwrote:
What is cin's streambuffer's state and content after the following
(using namespace std)?

string((istreambuf_iterator<char>(cin)),
istreambuf_iterator<char>()) );

I am trying to use this in a loop (to get a 'multiple cat'-like
behavior) and the second time around it returns an empty string
instead of waiting for more input. This makes me think that either the
end-of-stream stays in the buffer or cin or its buffer is in some bad
state. Either way, how can this be fixed to get more input?

Thanks,
S.
What was wrong with using the get unformatted text methods (get or
getline) of cin?
Then checking the state via cin::bad(), cin::fail(), cin::eof(), and
cin::good()?
Feb 22 '08 #2
On Feb 22, 2:20 pm, Sanyi <Sandor.Benc...@gmail.comwrote:
What is cin's streambuffer's state and content after the following
(using namespace std)?
string((istreambuf_iterator<char>(cin)),
istreambuf_iterator<char>()) );
I am trying to use this in a loop (to get a 'multiple cat'-like
behavior) and the second time around it returns an empty string
instead of waiting for more input.
What else do you expect. The first time around, you read until
end of file. What do you expect to read further once you've
reached end of file?
This makes me think that either the end-of-stream stays in the
buffer or cin or its buffer is in some bad state.
Once you've reached end of file, you've reached end of file,
yes. And once a read has failed, the istream remains in failed
state until you clear the error.
Either way, how can this be fixed to get more input?
What more input do you expect? It's easy to clear the failbit:
cin.clear(). But you're still at end of file, and there is
nothing more to read.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Feb 22 '08 #3
On Feb 22, 7:47 pm, Christopher <cp...@austin.rr.comwrote:
On Feb 22, 7:20 am, Sanyi <Sandor.Benc...@gmail.comwrote:
[...]
Moral of the story is stream buffer iterators differ from your
everyday iterator.
istreambuf_iterator is an input iterator. It fulfills all of
the requirements of an input iterator. It's not like a foreward
iterator, of course, but that's why there are different
categories.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Feb 23 '08 #4
Thanks for all for the enlightening replies, and sorry if I made some
rash statements.
In your original problem, the behavior of the "underlying sequence" of a
stream buffer on which eof occurred is not defined by the Standard and I
believe it cannot be defined usefully, because it is more as the
sequence is the active piece here (that is, the agent that creates or at
least originally detects eof condition), rather than your C++ program.
In your case, when the user presses Ctrl-D, s/he instructs the terminal
(which is not a part of your program) to stop sending characters to your
process for good -- and I am not aware of a standard way for a program
to re-connect to any terminal again after this happened.
That pretty much clears everything up. I had the wrong idea that the
end-of-file is just another character in the input stream that can be
neglected by my program if it wishes. (The reason why I chose it as
terminator was that in a first version my chunk of text was indeed
piped in, and only later I wanted to extend it to a interactive multi-
chunk piece. Also it seemed to me that is an easy way to abstract some
differences btw linux and windows/dos.)
I see now, that I better use some other terminating sequence as
recommended below.

As for respecting unix conventions, I will remember next time. Given
the very limited user space, in this case it should have not been a
problem.

Thanks,
S.
What I believe you are trying to achieve is to implement some protocol
to allow the user to instruct the program to process the piece of data
s/he already entered and then wait for another piece. Ctrl-D will not
work for this and maybe it is not at all bad because, if it did, you
would have another problem -- how to notify the program that the user is
done-done. You need to introduce your own protocol.

This problem is not new either, so there used to be some "standards" in
UNIX (and not only) for this part of console user interface and some of
your older users may be comfortable if you use these standards. For
example I vaguely remember these (it was so long ago that I do not
remember what programs did it, one with dot was probably some primitive
mail client):

1. Enter a text line with a single dot character (these days, ClearCase
command-line tool does it, too)
2. Enter 2 empty lines in a row

Then, to actually "disconnect" of your program user would have to enter
Ctrl-D or maybe enter 2 dot-only lines in a row or whatever you decide.

Hope this will help
-Pavel
Feb 24 '08 #5
Thanks for all for the enlightening replies, and sorry if I made some
rash statements.
In your original problem, the behavior of the "underlying sequence" of a
stream buffer on which eof occurred is not defined by the Standard and I
believe it cannot be defined usefully, because it is more as the
sequence is the active piece here (that is, the agent that creates or at
least originally detects eof condition), rather than your C++ program.
In your case, when the user presses Ctrl-D, s/he instructs the terminal
(which is not a part of your program) to stop sending characters to your
process for good -- and I am not aware of a standard way for a program
to re-connect to any terminal again after this happened.
That pretty much clears everything up. I had the wrong idea that the
end-of-file is just another character in the input stream that can be
neglected by my program if it wishes. (The reason why I chose it as
terminator was that in a first version my chunk of text was indeed
piped in, and only later I wanted to extend it to a interactive multi-
chunk piece. Also it seemed to me that is an easy way to abstract some
differences btw linux and windows/dos.)
I see now, that I better use some other terminating sequence as
recommended below.

As for respecting unix conventions, I will remember next time. Given
the very limited user space, in this case it should have not been a
problem.

Thanks,
S.
What I believe you are trying to achieve is to implement some protocol
to allow the user to instruct the program to process the piece of data
s/he already entered and then wait for another piece. Ctrl-D will not
work for this and maybe it is not at all bad because, if it did, you
would have another problem -- how to notify the program that the user is
done-done. You need to introduce your own protocol.

This problem is not new either, so there used to be some "standards" in
UNIX (and not only) for this part of console user interface and some of
your older users may be comfortable if you use these standards. For
example I vaguely remember these (it was so long ago that I do not
remember what programs did it, one with dot was probably some primitive
mail client):

1. Enter a text line with a single dot character (these days, ClearCase
command-line tool does it, too)
2. Enter 2 empty lines in a row

Then, to actually "disconnect" of your program user would have to enter
Ctrl-D or maybe enter 2 dot-only lines in a row or whatever you decide.

Hope this will help
-Pavel
Feb 24 '08 #6
Thanks for all for the enlightening replies, and sorry if I made some
rash statements.
In your original problem, the behavior of the "underlying sequence" of a
stream buffer on which eof occurred is not defined by the Standard and I
believe it cannot be defined usefully, because it is more as the
sequence is the active piece here (that is, the agent that creates or at
least originally detects eof condition), rather than your C++ program.
In your case, when the user presses Ctrl-D, s/he instructs the terminal
(which is not a part of your program) to stop sending characters to your
process for good -- and I am not aware of a standard way for a program
to re-connect to any terminal again after this happened.
That pretty much clears everything up. I had the wrong idea that the
end-of-file is just another character in the input stream that can be
neglected by my program if it wishes. (The reason why I chose it as
terminator was that in a first version my chunk of text was indeed
piped in, and only later I wanted to extend it to a interactive multi-
chunk piece. Also it seemed to me that is an easy way to abstract some
differences btw linux and windows/dos.)
I see now, that I better use some other terminating sequence as
recommended below.

As for respecting unix conventions, I will remember next time. Given
the very limited user space, in this case it should have not been a
problem.

Thanks,
S.
What I believe you are trying to achieve is to implement some protocol
to allow the user to instruct the program to process the piece of data
s/he already entered and then wait for another piece. Ctrl-D will not
work for this and maybe it is not at all bad because, if it did, you
would have another problem -- how to notify the program that the user is
done-done. You need to introduce your own protocol.

This problem is not new either, so there used to be some "standards" in
UNIX (and not only) for this part of console user interface and some of
your older users may be comfortable if you use these standards. For
example I vaguely remember these (it was so long ago that I do not
remember what programs did it, one with dot was probably some primitive
mail client):

1. Enter a text line with a single dot character (these days, ClearCase
command-line tool does it, too)
2. Enter 2 empty lines in a row

Then, to actually "disconnect" of your program user would have to enter
Ctrl-D or maybe enter 2 dot-only lines in a row or whatever you decide.

Hope this will help
-Pavel
Feb 24 '08 #7
I am sorry for the triple(!?) reply.

Feb 24 '08 #8

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

Similar topics

10
by: William Payne | last post by:
Hello, when I was writing a user-driven test program for a data structure I wrote, I encountered an annoying problem. The test program is laid out as a menu with several numbered options. The user...
5
by: Chris Mantoulidis | last post by:
Let's say I have this: std::string s1; std::cin >> s1; This will read s1 from cin until it finds a space (or a newline, whichever comes first). Okay this works. But when I want to continue...
3
by: yw | last post by:
Hi, When I use std::cin and std::cout to input some data, how can I ingore the cin by using the Enter key? For example: string sname; cout<<"Please input your name:"<<endl; cin>>sname; If...
3
by: puzzlecracker | last post by:
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); I have seen it in faq - what does it do, exactly?
3
by: moleskyca1 | last post by:
In C++ FAQ (15.5), I see this code: int i = 0; while (std::cin >x) { // RIGHT! (reliable) ++i; // Work with x ... } but I don't know how can while loop end? ostream &operator >>(...) return...
8
by: junw2000 | last post by:
Below is simple code: #include <iostream> int main(){ int i; while(1){ while(!(std::cin >i)){ std::cout<<"Invalid input i: "<<i<<'\n';
8
by: Johannes Meng | last post by:
Good day, I'm experimenting with unbuffered input at the moment. To get input I basically use cin.get(). My problem are control sequences preceeded by an ESC character (i.e. up, down, f-keys et...
3
by: Ralf Goertz | last post by:
Hi, consider the following program #include <iostream> #include <fstream> using namespace std; int main(int argc, char *argv){
12
by: pekka | last post by:
I'm trying to measure user input time with my Timer class object. It isn't as easy as I expected. When using std::cin between timer start and stop, I get zero elapsed time. For some unknown reason,...
3
by: Alex Snast | last post by:
hello guys I need to modify the std::cin delim char from the default ' ' and '\n' characters to ',' i know that i can edit the delim in the getline command however i'd like to know if there's...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.