What is cin's streambuffer's state and content after the following
(using namespace std)?
string((istream buf_iterator<ch ar>(cin)),
istreambuf_iter ator<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. 7 5112
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((istream buf_iterator<ch ar>(cin)),
istreambuf_iter ator<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()?
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((istream buf_iterator<ch ar>(cin)),
istreambuf_iter ator<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 objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
On Feb 22, 7:47 pm, Christopher <cp...@austin.r r.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_iter ator 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 objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
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
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
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
I am sorry for the triple(!?) reply. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 selects one option by typing in
its corresponding number (int). Depending on the choice he made, he may be
asked to provide another integer (or no input at all). So all the user does
is entering integers. I don't want the user to be able to cause...
|
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 reading it reads what's
left over in the cin, and well that's logical.
|
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 I don't wan't to input anything, just press enter key to ingnore the cin
|
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?
|
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 ostream &. how can that ever end the while loop? Is it because
there is hidden operator to cast to int? I cannot figure out, please
help?
| |
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';
|
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 cetera). They basically look like
this: <ESC><EXCODE><CODE>, where EXCODE is a number describing the type of
key pressed and CODE is the actual key. I want to be able to decide whether
ESC or such a control key was pressed. Now if I do it like...
|
by: Ralf Goertz |
last post by:
Hi,
consider the following program
#include <iostream>
#include <fstream>
using namespace std;
int main(int argc, char *argv){
|
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, the clock seems to stop
ticking during execution of std::cin.
Here's my code:
#include <ctime>
#include <iostream>
#include <string>
|
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 something build in with me
having to overload the std::cin operator
thanks, Alex Snast.
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |