Topic: stringstream >> how to turn it on?


Author: Pete Becker <petebecker@acm.org>
Date: 1998/01/26
Raw View
Phlip wrote:
>
> /Ola mis amiges/
>
> For a while now I have been converting numbers to text using
> 'stringstream <<', and I got fairly smug about it.
>
> Then the urge overcame me to convert a string to a number. Death to
> 'sscanf'. I attempt this:
>
>     stringstream z;
>     int out;
>     z.str () = "666";
>     assert (z >> out);  //  bang
>     assert (666 == out);  //  bang
>
> Nope.

istringstream z("666");
int out;
z >> out;
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: "Brock" <peabody@npcinternational.com>
Date: 1998/01/26
Raw View
Phlip wrote in message <34C91623.95939513@deltanet.com>...
>    stringstream z;
>    int out;
>    z.str () = "666";
>    assert (z >> out);  //  bang
>    assert (666 == out);  //  bang
>

Try:

stringstream z;

z << "666";

int out;

z >> out;
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: "Phlip" <tegan@deltanet.com>
Date: 1998/01/27
Raw View
Brock wrote:

>Phlip wrote:

>>    stringstream z;
>>    int out;
>>    z.str () = "666";
>>    assert (z >> out);  //  bang
>>    assert (666 == out);  //  bang


[please notice the 'assert's are merely illustrative]

>Try:
>
>stringstream z;
>
>z << "666";
>
>int out;
>
>z >> out;

This did not work.

Pete Becker suggested:

> istringstream z("666");
> int out;
> z >> out;

which also did not work.

I can't use 'strstream' because it is obsolete, and I don't want to
mix and match old and new libraries in a production program.

Are we up against a bug in the Dinkumware Standard Library??

  --  Phlip
======= http://users.deltanet.com/~tegan/home.html =======
  --  Work is the source of the root of all evil  --
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: "Phlip" <tegan@deltanet.com>
Date: 1998/01/29
Raw View
End of crisis :-)

>> istringstream z("666");
>> int out;
>> z >> out;

It seems this design, forwarded by "lruss" and Pete Becker, works
better than the VC++5 debugger will admit.

For future reference, if a debugger shows you a number you don't
believe, always get a second opinion!!

8-P

  --  Phlip
======= http://users.deltanet.com/~tegan/home.html =======
  --  Work is the source of the root of all evil  --
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: Phlip <tegan@deltanet.com>
Date: 1998/01/23
Raw View
/Ola mis amiges/

For a while now I have been converting numbers to text using
'stringstream <<', and I got fairly smug about it.

Then the urge overcame me to convert a string to a number. Death to
'sscanf'. I attempt this:

    stringstream z;
    int out;
    z.str () = "666";
    assert (z >> out);  //  bang
    assert (666 == out);  //  bang

Nope.

What is the canonical example? Is the 'rdbuf' involved?

I'm using VC++5 with its Dinkumware Standard Library.

  --  Phlip
======= http://users.deltanet.com/~tegan/home.html =======
  --  Add the text NOSPAM to my address before replying  --
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: "Bradd W. Szonye" <bradds@concentric.net>
Date: 1998/01/24
Raw View
Phlip wrote in message <34C91623.95939513@deltanet.com>...
>I attempt this [to convert a string to a number]:
>
>    stringstream z;
>    int out;
>    z.str () = "666";
>    assert (z >> out);  //  bang
>    assert (666 == out);  //  bang

I can explain why this fails; I can't state whether it's conforming behavior
without research. The number parser is peeking ahead in the stream to find
out whether the next character fits the numeric format. After the third '6',
it peeks ahead and finds end-of-file. This sets the end-of-file flag, and
apparently also the fail flag.

I think that reading a number right up to end-of-stream is supposed to be
allowed, but in practice it often isn't. I also think this ties into the
Unix assumption that all text files end with an end-of-line marker.

To make an "stoi" function that works both in theory and in practice:

    int stoi(std::string s)
    {
        std::istringstream z(s + '\n');
        int ret;
        // this should use "no-skip-ws" but I can't remember the syntax
        z >> ret;
        assert (z); // didn't fail
        assert (z.get() == '\n'); // tail contains only what you put in
        assert (z.get() == std::string::traits::eof());
        return ret;
    }

If you're not concerned about the string containing only a number, then
don't worry about no-skip-ws or checking the tail of the string for a lone
newline.

If your standard library is good enough, you might also be able to use a
numeric formatter directly. Unfortunately, I've forgotten how to use them
(and whether that's even directly possible).

Now, for question: is >> supposed to work even if the number has no trailing
whitespace? I think it is supposed to work, but I'm not sure, and I don't
especially feel like looking it up right now. I should keep a notebook with
the answers to these sorts of questions.
---
Bradd W. Szonye
bradds@concentric.net
http://www.concentric.net/~Bradds




[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: "Michael S. Tsirkin" <mtsirkin@iil.intel.com>
Date: 1998/01/25
Raw View
>     stringstream z;
>     int out;
>     z.str () = "666";
>     assert (z >> out);  //  bang
>     assert (666 == out);  //  bang
>
> Nope.

I wouldn't recommend "assert (z >> out);" because this does nothing if
the NDEBUG macro is defined (in MSVC++ default in "Release" mode).
so your program will do different things in release and debug mode.


Regards,
 MST
--
Michael S. Tsirkin Intel, Haifa, Israel; Mail stop: IDC-4C
Work telephone in Israel 435-5658 ( Local in Israel 04-8655658 )
mailto:mtsirkin@iil.intel.com ; http://www.iil.intel.com/~mtsirkin/
    >   Four things are to be strengthened : Torah, and good deeds,
    >   prayer and one's good manners (Berachoth)
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: "Michael S. Tsirkin" <mtsirkin@iil.intel.com>
Date: 1998/01/25
Raw View
a bug in VC++5.0 documentation:

The documentation in VC++5.0 says:

    basic_string<E, T, A>& str();

which is incorrect, since the actual include file <sstream> says:


 _Mystr str() const
  {return (_Sb.str()); }


Which in lay terms means that str() returns a copy of the buffer, not a
reference to it, so if you assign to it you do not change the stream.

So what you should do is:

z.str ("666");
which will set the buffer to the correct value.

HTH,
 MSt


Phlip wrote:
>
> /Ola mis amiges/
>
> For a while now I have been converting numbers to text using
> 'stringstream <<', and I got fairly smug about it.
>
> Then the urge overcame me to convert a string to a number. Death to
> 'sscanf'. I attempt this:
>
>     stringstream z;
>     int out;
>     z.str () = "666";
>     assert (z >> out);  //  bang
>     assert (666 == out);  //  bang
>
> Nope.
>
> What is the canonical example? Is the 'rdbuf' involved?
>
> I'm using VC++5 with its Dinkumware Standard Library.
>

--
Michael S. Tsirkin Intel, Haifa, Israel; Mail stop: IDC-4C
Work telephone in Israel 435-5658 ( Local in Israel 04-8655658 )
mailto:mtsirkin@iil.intel.com ; http://www.iil.intel.com/~mtsirkin/
    >   Four things are to be strengthened : Torah, and good deeds,
    >   prayer and one's good manners (Berachoth)


[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]