std string streams

Discussion in 'C++' started by Dylan, Aug 9, 2005.

  1. Dylan

    Dylan Guest

    Is there a string stream I can write into and read from?

    I've tried std::stringstream but it won't let me read from it.

    ie I can do this:

    int iData = 123
    std::stringstream ss;
    ss << 123;

    but not this afterwards:

    int iDataReadFromStream;
    iDataReadFromStream << ss;


    Is there a data structure that allows me to do both?

    thanks
    Dylan, Aug 9, 2005
    #1
    1. Advertising

  2. Dylan wrote:
    > ie I can do this:
    >
    > int iData = 123
    > std::stringstream ss;
    > ss << 123;
    >
    > but not this afterwards:
    >
    > int iDataReadFromStream;
    > iDataReadFromStream << ss;


    I'm no expert, but I'd say the latter doesn't work, because it calls the
    <<-operator for int and not for stringstream.
    Try:
    ss>>iDataReadFromStream;

    --
    If geiger counter does not click,
    the coffee, she is just not thick
    Sebastian Hungerecker, Aug 9, 2005
    #2
    1. Advertising

  3. In message <>, Dylan
    <> writes
    >Is there a string stream I can write into and read from?
    >
    >I've tried std::stringstream but it won't let me read from it.
    >
    >ie I can do this:
    >
    >int iData = 123
    >std::stringstream ss;
    >ss << 123;
    >
    >but not this afterwards:
    >
    >int iDataReadFromStream;
    >iDataReadFromStream << ss;


    ss >> iDataReadFromStream;

    Streams always go on the left.

    --
    Richard Herring
    Richard Herring, Aug 9, 2005
    #3
  4. Dylan

    Dylan Guest

    On Tue, 09 Aug 2005 15:34:25 +0200, Sebastian Hungerecker
    <> wrote:

    >Dylan wrote:
    >> ie I can do this:
    >>
    >> int iData = 123
    >> std::stringstream ss;
    >> ss << 123;
    >>
    >> but not this afterwards:
    >>
    >> int iDataReadFromStream;
    >> iDataReadFromStream << ss;

    >
    >I'm no expert, but I'd say the latter doesn't work, because it calls the
    ><<-operator for int and not for stringstream.
    >Try:
    >ss>>iDataReadFromStream;


    that doesn't work either...
    Dylan, Aug 9, 2005
    #4
  5. Dylan

    Dylan Guest

    On Tue, 9 Aug 2005 13:48:35 +0000 (UTC), Dylan
    <> wrote:

    >On Tue, 09 Aug 2005 15:34:25 +0200, Sebastian Hungerecker
    ><> wrote:
    >
    >>Dylan wrote:
    >>> ie I can do this:
    >>>
    >>> int iData = 123
    >>> std::stringstream ss;
    >>> ss << 123;
    >>>
    >>> but not this afterwards:
    >>>
    >>> int iDataReadFromStream;
    >>> iDataReadFromStream << ss;

    >>
    >>I'm no expert, but I'd say the latter doesn't work, because it calls the
    >><<-operator for int and not for stringstream.
    >>Try:
    >>ss>>iDataReadFromStream;

    >
    >that doesn't work either...


    forgive me, it does work.
    Dylan, Aug 9, 2005
    #5
  6. maybe your thinking of a situation like this...

    const int MaxSize = 1023;
    std::stringstream convert;
    char temp[MaxSize + 1] ={0};
    float val;

    // Say we have a control that has a "text" floating point value
    GetDlgItemText(IDC_SOME_CONTROL, temp, MaxSize);

    convert << temp;
    convert >> val;

    // if you forgot to call this afterward, you would have issues writing or
    reading from the stringstream
    convert.clear();
    convert.str("");

    "Dylan" <> wrote in message
    news:...
    > Is there a string stream I can write into and read from?
    >
    > I've tried std::stringstream but it won't let me read from it.
    >
    > ie I can do this:
    >
    > int iData = 123
    > std::stringstream ss;
    > ss << 123;
    >
    > but not this afterwards:
    >
    > int iDataReadFromStream;
    > iDataReadFromStream << ss;
    >
    >
    > Is there a data structure that allows me to do both?
    >
    > thanks
    >
    Joe Bacigalupa, Aug 9, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Peter Jansson
    Replies:
    5
    Views:
    6,272
    Ivan Vecerina
    Mar 17, 2005
  2. Vinu
    Replies:
    4
    Views:
    346
    Jim Langston
    Jul 7, 2005
  3. Fei Liu
    Replies:
    9
    Views:
    434
  4. Jeffrey Walton
    Replies:
    10
    Views:
    929
    Mathias Gaunard
    Nov 26, 2006
  5. Leslaw Bieniasz
    Replies:
    2
    Views:
    1,021
    Thomas J. Gritzan
    Jan 15, 2010
Loading...

Share This Page