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

istringstream: str() only works once?

I'm using an istringstream to convert some integers stored in string
form to integers. However, each integer is stored in a differnt
string. So, I used istringstream's str(string) method for the first
string and then used the >> operator to extract the integer. It
worked just fine. So, on to the second string. I used str(string)
again but this time extraction gave me a seemingly random number
(-858993460). Once an istringstream is initialized or str(string) is
use, can the stream's string not be changed?

-Brandon
Jul 22 '05 #1
4 2689

"Brandon" <bc******@creighton.edu> wrote in message
news:24**************************@posting.google.c om...
I'm using an istringstream to convert some integers stored in string
form to integers. However, each integer is stored in a differnt
string. So, I used istringstream's str(string) method for the first
string and then used the >> operator to extract the integer. It
worked just fine. So, on to the second string. I used str(string)
again but this time extraction gave me a seemingly random number
(-858993460). Once an istringstream is initialized or str(string) is
use, can the stream's string not be changed?

-Brandon


No, istringstreams work in exactly the same way as other streams.

What happens was that when you read the first integer from the stream you
hit the end of the stream, and so put your stream in an end of file state.
Once this happens, to any stream, nothing more will work on the stream until
you clear the end of file state. Use the clear() method to do this. Here's
some code

#include <iostream>
#include <sstream>
#include <string>

int main()
{
std::string one = "1";
std::string two = "2";
std::istringstream str(one);
int x;
str >> x; // now stream is in end of file state
str.clear(); // clear end of file state
str.str(two);
int y;
str >> y;
std::cout << x << ' ' << y << '\n';
}

john
Jul 22 '05 #2
> What happens was that when you read the first integer from the stream you
hit the end of the stream, and so put your stream in an end of file state.
Once this happens, to any stream, nothing more will work on the stream until you clear the end of file state. Use the clear() method to do this. Here's
some code

#include <iostream>
#include <sstream>
#include <string>

int main()
{
std::string one = "1";
std::string two = "2";
std::istringstream str(one);
int x;
str >> x; // now stream is in end of file state
str.clear(); // clear end of file state
str.str(two);
int y;
str >> y;
std::cout << x << ' ' << y << '\n';
}


Try the above code without the clear() and it doesn't work. Now replace "1"
with "1 " (note extra space). The extra space means you do not hit the end
of the stream when you read the first integer, and therefore the call to
clear() is no longer necessary.

john
Jul 22 '05 #3
> What happens was that when you read the first integer from the stream you
hit the end of the stream, and so put your stream in an end of file state.
Once this happens, to any stream, nothing more will work on the stream until
you clear the end of file state. Use the clear() method to do this.


I hadn't really thought about that but it makes perfect sense. It
does seem like a fairly bad design though. As you said, once you hit
the end of the stream, nothing more will work. That makes perfect
sense for methods like get(), read(), and extraction but if you are
trying to switch the string that it is using, you obviously won't be
at the end of the stream anymore (unless you give it give it "" of
course.)
Jul 22 '05 #4

"Brandon" <bc******@creighton.edu> wrote in message
news:24**************************@posting.google.c om...
What happens was that when you read the first integer from the stream you hit the end of the stream, and so put your stream in an end of file state. Once this happens, to any stream, nothing more will work on the stream until you clear the end of file state. Use the clear() method to do this.


I hadn't really thought about that but it makes perfect sense. It
does seem like a fairly bad design though. As you said, once you hit
the end of the stream, nothing more will work. That makes perfect
sense for methods like get(), read(), and extraction but if you are
trying to switch the string that it is using, you obviously won't be
at the end of the stream anymore (unless you give it give it "" of
course.)


It gets worse. Even closing a file and then opening a new one doesn't reset
a streams state. So an error on a previous file will stop a new file working
if you use the same variable.

I agree it seems nonsensical, apparently the situation with file streams is
being reviewed.

john
Jul 22 '05 #5

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

Similar topics

8
by: Agent Mulder | last post by:
I try to remove the spaces from a string using an old trick that involves an istringstream object. I expect the while-condition while(istringstream>>string) to evaluate to false once the...
3
by: bml | last post by:
Could you help and answer my questions of istringstream? Thanks a lot! 1. Reuse an "istringstream" istringstream ist; ist.str("This is FIRST test string"); ist.str("This is SECOND test...
7
by: Luther Baker | last post by:
Hi, My question is regarding std::istringstream. I am serializing data to an ostringstream and the resulting buffer turns out just fine. But, when I try the reverse, when the istringstream...
6
by: JustSomeGuy | last post by:
I am passing an istringstream to a function. I want that function to get a copy of the istringstream and not a refrence to it. ie when the function returns I want the istringstream to be...
11
by: icanoop | last post by:
I would like to do this MyClass x; istringstream("XXX") >> x; // Works in VC++ but not GCC instead of MyClass x; istringstream iss("XXX"); iss >> x; // Works in both GCC and VC++
10
by: k:arel | last post by:
today i've discovered the istringstream object i'm trying to read a string and split it up in a number of fields, consisting of integers and strings it's for a protocol that sends messages with:...
7
by: mathieu | last post by:
Hello, Is there a way to construct a std::istringstream from a buffer of char and avoid copying the array of bytes ? const char s = "Hello, World"; std::string str(s); std::istringstream is;...
2
by: rolan | last post by:
I'm trying to figure out how to convert an array of type "string" into integers, and I have begun using istringstream. I made a program to test how istringstream works, and I've been having...
12
by: sergey.lukoshkin | last post by:
Hello everyone! My task is in converting numbers from string to int variables. I used istringstream to perform it. So I wrote simple test function. But it doesn't work as I expected because...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
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: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.