How to use stringstream more than once?

I can successfully use a stringstream once like so:

1
2
3
4
std::stringstream lconvert;
lconvert << lvalstr;
lconvert >> lppos->mSector.x;
lconvert >> lppos->mSector.y;


but if I try to use it again with another string, I don't get the correct behavior. I know the string is valid because if I declare a new stringstream it formats just fine
1
2
3
4
//second time through, values are not read in from lconvert
lconvert << lvalstr;
lconvert >> (*lptrans)[0][0];
lconvert >> (*lptrans)[0][1];


1
2
3
4
5
//second time through, this works if i use a new stringstream
std::stringstream lconvert2;
lconvert2 << lvalstr;
lconvert2 >> (*lptrans)[0][0];
lconvert2 >> (*lptrans)[0][1];


Is the solution to just use a stringstream* and just allocate/delete each time I need a stream?

Thanks
Is the solution to just use a stringstream* and just allocate/delete each time I need a stream?
¿but why will you use dynamic allocation for that? lconvert = std::stringstream(lvalstr);
You could also set the value with str but you will need to call clear too. (error prone)

The problem is that after you finish reading those values the stream remains in an error state, and the later insertion fails.
I see first I was trying to do it with lconvert.flush().

If I call lconvert.clear() at the end of the first time it works now.

Do I need to make sure the stream has been emptied by the time I call .clear() to prevent errors?
Topic archived. No new replies allowed.