istringstream::str and clear

M

Marc

Hello,

I noticed today that istringstream::str, which changes the underlying
string, does not clear the error state. In particular, if you read a
string to the end then change the string and try to read the new one,
eofbit is still set so it fails. In a similar situation, ifstream::eek:pen
calls clear when you open a new file. Is there a particular reason for
this inconsistency?

int n;
istringstream i("1");
i >> n;
i.str("2");
i >> n; // fails because we still have i.eof()
 
P

Paul

Marc said:
Hello,

I noticed today that istringstream::str, which changes the underlying
string, does not clear the error state. In particular, if you read a
string to the end then change the string and try to read the new one,
eofbit is still set so it fails. In a similar situation, ifstream::eek:pen
calls clear when you open a new file. Is there a particular reason for
this inconsistency?

int n;
istringstream i("1");
i >> n;
i.str("2");
i >> n; // fails because we still have i.eof()
I just a quick look up an online reference for this and according to:
http://www.cplusplus.com/reference/iostream/istringstream/str/

str calls rdbuf(), well it say it "effectively" calls rdbuf(), whatever that
means exactly. If it does call rdbuf then rdbuf should set the ios flags to
goodbit.
 
P

Paul

cg_chas said:
then it goes on to say that, "Notice that setting a new string does not
clear
the error flags currently set in the stream object unless the member
function
clear is explicitly called."
Yes indeed ,
And having just tried it out on my implementation I can clarify that I get
the same behaviour.
I can still retrieve the internal data by calling str() instead of using >>.
 
A

Arvind

then it goes on to say that, "Notice that setting a new string does not clear
the error flags currently set in the stream object unless the member function
clear is explicitly called."

Why would someone want to set the new string in istringstream object
and does not want to reset the error flags? If I want to change the
content of istringstream object, then certainly next step would be to
read it.
 
P

Paul

then it goes on to say that, "Notice that setting a new string does not
clear
the error flags currently set in the stream object unless the member
function
clear is explicitly called."

Why would someone want to set the new string in istringstream object
and does not want to reset the error flags? If I want to change the
content of istringstream object, then certainly next step would be to
read it.
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

I think the intention is that when str() is used the stream is frozen and
the stringstream should be treated like a string , not a stream.

I was searching to find a practical use of stringstream, which I did not
find, but I found the following little msdn explanation that helped me
understand it a bit.
http://msdn.microsoft.com/en-us/library/d653w0hz(v=VS.71).aspx
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top