Strange problem of typecasting with stringstream | | |
I was trying to cast some strings to integers with stringstream, then
this strange problem poped up.
Here is my test code:
stringstream conv;
string from;
int to;
from = "1";
conv << from;
conv >to;
from = "2";
conv << from;
conv >to;
The first casting worked fine, but the second casting didn't work at
all.
conv.str() returned "1" after 'from = "2"; conv << from;' was executed.
Then I explicitly changed the buffer of conv, like this:
conv.str("1");
conv >to;
conv.str("2");
conv >to;
Still, the first casting worked perfectly, the second one failed, to ==
1.
I'm wondering I've terribly misunderstanding something, sorry if I'm
asking the obvious. | | | | re: Strange problem of typecasting with stringstream
<vigacmoe@gmail.comwrote in message
news:1160451343.468535.83540@c28g2000cwb.googlegro ups.com... Quote:
>I was trying to cast some strings to integers with stringstream, then
this strange problem poped up.
Here is my test code:
>
stringstream conv;
string from;
int to;
>
from = "1";
conv << from;
conv >to;
>
from = "2";
conv << from;
conv >to;
>
The first casting worked fine, but the second casting didn't work at
all.
conv.str() returned "1" after 'from = "2"; conv << from;' was executed.
Then I explicitly changed the buffer of conv, like this:
>
conv.str("1");
conv >to;
>
conv.str("2");
conv >to;
>
Still, the first casting worked perfectly, the second one failed, to ==
1.
I'm wondering I've terribly misunderstanding something, sorry if I'm
asking the obvious.
I"m suspecting that the stringstream is having "12" instead of "1 2" or
soemthing. I would try the following til I found what worked.
from = "1";
conv << from;
conv >to;
from = " 2";
conv << from;
conv >to;
if that didnt' work see what
from = "1 2";
conv << from;
conv >to;
conv >to;
gives you.
You may need to reset the stringstream in between, but I've never had that
problem. | | | | re: Strange problem of typecasting with stringstream vigacmoe@gmail.com wrote: Quote:
I was trying to cast some strings to integers with stringstream, then
this strange problem poped up.
Here is my test code:
>
stringstream conv;
string from;
int to;
>
from = "1";
conv << from;
conv >to;
Try:
conv.clear(); Quote:
>
from = "2";
conv << from;
conv >to;
Regards,
Sumit. | | | | re: Strange problem of typecasting with stringstream
Jim Langston wrote: Quote:
from = "1";
conv << from;
conv >to;
>
from = " 2";
conv << from;
conv >to;
>
didn't work.
from = " 2";
conv << from;
didn't change the inner buffer of conv, conv.str() still returned "1". Quote:
if that didnt' work see what
>
from = "1 2";
conv << from;
conv >to;
conv >to;
>
this worked. Quote:
gives you.
>
You may need to reset the stringstream in between, but I've never had that
problem.
| | | | re: Strange problem of typecasting with stringstream
Sumit Rajan wrote: Quote: vigacmoe@gmail.com wrote: Quote:
I was trying to cast some strings to integers with stringstream, then
this strange problem poped up.
Here is my test code:
stringstream conv;
string from;
int to;
from = "1";
conv << from;
conv >to;
>
Try:
conv.clear();
yes, conv.clear() did the job, but why the previous method doesn't
work?
I've done some test, it seems that if conv's internal pointer ever
pointed to the end of its buffer, the next << operation would fail.
These code worked as intended:
conv << "1"; /* conv.str() = "1", conv.tellg() = 0 */
conv << " 2"; /* conv.str() = "1 2", conv.tellg() = 0 */
conv >to; /* conv.str() = "1 2", conv.tellg() = 1, to =
1*/
conv << " 3"; /* conv.str() = "1 2 3", conv.tellg() = 1 */
conv >to; /* conv.str() = "1 2 3", conv.tellg() = 3, to =
2 */
but these didn't
conv1 << "1"; /* conv.str() = "1", conv.tellg() = 0 */
conv1 << " 2"; /* conv.str() = "1 2", conv.tellg() = 0 */
conv1 >to; /* conv.str() = "1 2", conv.tellg() = 1, to =
1 */
conv1 >to; /* conv.str() = "1 2", conv.tellg() = 3, to =
2, internal pointer reached the end */
conv1 << " 3"; /* conv.str() = "1 2", conv.tellg() = -1, <<
operation failed */
conv1 >to; /* conv.str() = "1 2", conv.tellg() = -1, to =
2 */
I did the test on MS Visual Studio .NET 2003, is this a bug of the
standard library implementation I'm using? Quote:
> Quote:
from = "2";
conv << from;
conv >to;
>
Regards,
Sumit.
| | | | re: Strange problem of typecasting with stringstream vigacmoe@gmail.com wrote: Quote:
I was trying to cast some strings to integers with stringstream, then
this strange problem poped up.
Here is my test code:
Doesn't compile. Please post complete programs, see http://www.parashift.com/c++-faq-lit...t.html#faq-5.8 Quote:
>
stringstream conv;
string from;
int to;
>
from = "1";
conv << from;
conv >to;
So what do you think happens in the last line. To be like the library,
you have to think like the library :-) You read all characters upto and
including EOF. Thus setting state |= eofbit in conv. Thus making the
stream invalid for all further operations. Quote:
>
from = "2";
conv << from;
conv >to;
The << operator returns without doing anything, because your eofbit is
set. The >operator returns without doing anything, because the eofbit
is set. Quote:
conv.str() returned "1" after 'from = "2"; conv << from;' was executed.
Well, conv << from; was a NOP for the reasons mentioned. conv.str() is
"1" before you do nothing. It will still return "1" after you do
nothing. Quote:
Then I explicitly changed the buffer of conv, like this:
>
conv.str("1");
conv >to;
>
conv.str("2");
conv >to;
>
Still, the first casting worked perfectly, the second one failed, to ==
1.
There is no casting in your code. You probably have a scripting
language background, like perl. The terms we use in C++ for read
operations is something like "the first read". Casting is a different
concept and quite a subject in itself. Btw, if you take care not to
read EOF by adding a whitespace like " " or "\n", everything works
fine.
#include <sstream>
#include <iostream>
int main () {
std::stringstream conv("1\n");
int to;
conv >to;
std::cout << to << "\n";
conv.str("2\n");
conv >to;
std::cout << to << "\n";
} Quote:
I'm wondering I've terribly misunderstanding something, sorry if I'm
asking the obvious.
Your doing fine. You are just a little bit careless. And you should use
an IDE with a debugger. If you singlestep your program, the state
member of your stringstream will instantly tell you what's happend. | | | | re: Strange problem of typecasting with stringstream
Gan Quan wrote in message
<1160475494.445189.201170@m7g2000cwm.googlegroups. com>... Quote:
>
>Sumit Rajan wrote:
> Quote:
> vigacmoe@gmail.com wrote: Quote:
I was trying to cast some strings to integers with stringstream, then
this strange problem poped up.
Here is my test code:
>
stringstream conv;
string from;
int to;
>
from = "1";
conv << from;
conv >to;
>>
>Try:
>conv.clear();
>yes, conv.clear() did the job, but why the previous method doesn't
>work?
>I've done some test, it seems that if conv's internal pointer ever
>pointed to the end of its buffer, the next << operation would fail.
>These code worked as intended:
conv << "1"; /* conv.str() = "1", conv.tellg() = 0 */
conv << " 2"; /* conv.str() = "1 2", conv.tellg() = 0 */
conv >to; /* conv.str() = "1 2", conv.tellg() = 1, to =
>1*/
conv << " 3"; /* conv.str() = "1 2 3", conv.tellg() = 1 */
conv >to; /* conv.str() = "1 2 3", conv.tellg() = 3, to =
>2 */
>but these didn't
conv1 << "1"; /* conv.str() = "1", conv.tellg() = 0 */
conv1 << " 2"; /* conv.str() = "1 2", conv.tellg() = 0 */
conv1 >to; /* conv.str() = "1 2", conv.tellg() = 1, to =
>1 */
conv1 >to; /* conv.str() = "1 2", conv.tellg() = 3, to =
>2, internal pointer reached the end */
conv1 << " 3"; /* conv.str() = "1 2", conv.tellg() = -1, <<
>operation failed */
conv1 >to; /* conv.str() = "1 2", conv.tellg() = -1, to =
>2 */
>I did the test on MS Visual Studio .NET 2003, is this a bug of the
>standard library implementation I'm using?
No, I don't think so. See my example below.
std::stringstream conv;
std::string from;
int to;
from = "1";
conv << from;
conv >to;
std::cout<<conv.str()<<" to="<<to<<std::endl;
// stringstream is now in a 'failed' state.... (sort of<G>)
from = "2";
conv << from;
conv >to;
std::cout<<conv.str()<<" to="<<to<<std::endl;
// ...so, trying to set it to "2" fails.
conv.clear(); // reset the stream to a 'good' state
from = "2";
conv << from;
conv >to;
std::cout<<conv.str()<<" to="<<to<<std::endl;
// see below
conv.str(""); // 'clear' the buffer
conv.clear(); // and 'reset' it.
from = "2";
conv << from;
conv >to;
std::cout<<conv.str()<<" to="<<to<<std::endl;
/* -- output --
1 to=1
1 to=1
12 to=2 // using only '.clear()'
2 to=2
*/
--
Bob R
POVrookie | | | | re: Strange problem of typecasting with stringstream
BTW, if you do a whole bunch of string-to-number/number-to-string converting,
you may want these templates (put them in header file):
[ these were in a FAQ, but, I couldn't remember which one[1], so, I post
it.<G>]
#include <iostream>
// ------- any number to string, string to any number -------
#include <string>
#include<sstream>
#include <iomanip // for 'setprecision()'
template<typename TT FromString( std::string const &s){
std::istringstream is( s ); T t; is >t; return t;
} // FromString(string&)
template<typename Tstd::string ToString( T const &t, size_t prec = 6){
// std::ostringstream s; s << t; return s.str(); // original
std::ostringstream s;
s.setf(std::ios_base::fixed);
s <<std::setprecision( prec )<< t;
return s.str();
} // ToString(T&,size_t)
// ------------------------------------------------------
void TestPrint(){
using std::cout;
double Dnum(12345.6789);
std::string txt = ToString( Dnum );
double Dnum2 = FromString<double>( txt );
cout<<" ------- "<<std::endl;
cout<<" Dnum="<<Dnum<<std::endl;
cout<<" txt="<<txt<<std::endl;
cout<<" Dnum2="<<Dnum2<<std::endl;
cout<<" ------- "<<std::endl;
int Dnum3(12345);
txt = ToString( Dnum3 );
int Dnum4 = FromString<int>( txt );
cout<<" Dnum3="<<Dnum3<<std::endl;
cout<<" txt="<<txt<<std::endl;
cout<<" Dnum4="<<Dnum4<<std::endl;
cout<<" ------- "<<std::endl;
return;
} // TestPrint()
int main(){
TestPrint();
return 0;
} // main()
[1] - CRS and Sometimers!
--
Bob R
POVrookie | | | | re: Strange problem of typecasting with stringstream
BobR schrieb: Quote:
>
BTW, if you do a whole bunch of string-to-number/number-to-string converting,
you may want these templates (put them in header file):
[ these were in a FAQ, but, I couldn't remember which one[1], so, I post
it.<G>]
[snipped code]
It's not the same code you posted, but it handles bad conversion:
From 39.1 to 39.3: http://www.parashift.com/c++-faq-lit...al-issues.html
--
Thomas http://www.netmeister.org/news/learn2quote.html | | | | re: Strange problem of typecasting with stringstream
Thomas J. Gritzan wrote in message ... Quote:
>BobR schrieb: Quote:
>>
>BTW, if you do a whole bunch of string-to-number/number-to-string
converting, Quote: Quote:
>you may want these templates (put them in header file):
>[ these were in a FAQ, but, I couldn't remember which one[1], so, I post
>it.<G>]
>[snipped code]
>
>It's not the same code you posted, but it handles bad conversion:
>
>From 39.1 to 39.3:
> http://www.parashift.com/c++-faq-lit...al-issues.html Guess what? I clicked your link while off-line and up-it-poped! That was what
I was looking for, thanks. [d a r n CRS!!]
Now I only have to do a search for what dang partition/folder I put it in!
[..and move it to the proper place - in my library.]
--
Bob R
POVrookie | | | | re: Strange problem of typecasting with stringstream
F.J.K. 写道: Quote: vigacmoe@gmail.com wrote: Quote:
I was trying to cast some strings to integers with stringstream, then
this strange problem poped up.
Here is my test code:
>
Doesn't compile. Please post complete programs, see http://www.parashift.com/c++-faq-lit...t.html#faq-5.8
> Quote:
stringstream conv;
string from;
int to;
from = "1";
conv << from;
conv >to;
>
So what do you think happens in the last line. To be like the library,
you have to think like the library :-) You read all characters upto and
including EOF. Thus setting state |= eofbit in conv. Thus making the
stream invalid for all further operations.
Thanks, this explains everything, I didn't notice the state change,
though I did notice that once the internal pointer reach the end of the
buffer, the next read/write operation would fail ;) Quote:
> Quote:
from = "2";
conv << from;
conv >to;
>
The << operator returns without doing anything, because your eofbit is
set. The >operator returns without doing anything, because the eofbit
is set.
> Quote:
conv.str() returned "1" after 'from = "2"; conv << from;' was executed.
>
Well, conv << from; was a NOP for the reasons mentioned. conv.str() is
"1" before you do nothing. It will still return "1" after you do
nothing.
> Quote:
Then I explicitly changed the buffer of conv, like this:
conv.str("1");
conv >to;
conv.str("2");
conv >to;
Still, the first casting worked perfectly, the second one failed, to ==
1.
>
There is no casting in your code. You probably have a scripting
language background, like perl. The terms we use in C++ for read
operations is something like "the first read". Casting is a different
concept and quite a subject in itself. Btw, if you take care not to
read EOF by adding a whitespace like " " or "\n", everything works
fine.
>
#include <sstream>
#include <iostream>
>
int main () {
std::stringstream conv("1\n");
int to;
conv >to;
std::cout << to << "\n";
conv.str("2\n");
conv >to;
std::cout << to << "\n";
}
> Quote:
I'm wondering I've terribly misunderstanding something, sorry if I'm
asking the obvious.
>
Your doing fine. You are just a little bit careless. And you should use
an IDE with a debugger. If you singlestep your program, the state
member of your stringstream will instantly tell you what's happend.
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,414 network members.
|