Trouble using the stringstream conversion for numbers/strings

Hey,


i am using the string-to-number conversion due stringstreams in a loop.
i was wondering since it did only work the first time the loop runs.
after a while(way toooo long) i found out, that always when i do stringstream >> number while inside the stringstream is e.g.'41\0' the errorbit was set.
So i had to clear this everytime the loop runs...

why?
streamed the 41 into the number-variable?
is this because the stream "file pointer" reaches its last position or the '\0' after it
The simplest explanation is that you are trying to extract too many numbers from the stream. From your problem description, it would seem your stringstream contains only one number, and you are of course trying to extract several in your loop.

If you are at the end of the stream and you tried to extract another number, then the failbit will be set. I'm not sure if this is what exactly what you meant, but if your entire stream is just "41\0", then that contains only one numeric value (41) before the string is null-terminated, so naturally you can only extract one number.

Post your code, so we can see exactly what your stream looks like before you try to extract the values.
1
2
3
4
5
6
7
8
9
10
11
12
13
do
	{
		pos = strstr(fpos, ";x;");
		memcpy(secondary,fpos,(pos-fpos));
		secondary[pos-fpos] = '\0';
		s.assign(secondary);
		str.str(s);
		str >> j;
		str.clear();
		numbers.push_back(j);
		logfile << secondary << "\n";
		fpos = pos+3;
	}while((pos - buffer) < (filesize-3));


where secondary is an char-array, str is from type "stringstream", numbers is an vector<int> and logfile is and fstream, fpos and pos are char* and s is a string;


i know this looks over-complicated, but i was testing something...

and without the str.clear() this does not work the second time, because after
str << j;
the stream enters the error state....
Topic archived. No new replies allowed.