istringstream question

Discussion in 'C++' started by Viet Le Hong, Sep 18, 2003.

  1. Viet Le Hong

    Viet Le Hong Guest

    Hi,
    Is there any way to initiate a istringstream object's string without
    using constructor. I have tried the str() function but it did not work.
    For example, I want to write the following code:

    istreamstring iss(my_string);

    then do somethings that involes the >> operator

    then I want to change the string content in this buffer to other string
    and use the >> operator on it again.

    I have tried iss.str(my_new_string), but it does not work.Anybody knows
    how to do this?

    Thanks very much
    Viet
    Viet Le Hong, Sep 18, 2003
    #1
    1. Advertising

  2. Viet Le Hong

    Buster Guest

    "Viet Le Hong" <> wrote
    > Hi,
    > Is there any way to initiate a istringstream object's string without
    > using constructor. I have tried the str() function but it did not work.
    > For example, I want to write the following code:
    >
    > istreamstring iss(my_string);
    >
    > then do somethings that involes the >> operator
    >
    > then I want to change the string content in this buffer to other string
    > and use the >> operator on it again.
    >
    > I have tried iss.str(my_new_string), but it does not work.Anybody knows
    > how to do this?


    That is how you do it. It works for me. Post a complete, compilable code
    sample and tell us where the error occurs.
    Buster, Sep 18, 2003
    #2
    1. Advertising

  3. Viet Le Hong

    Viet Le Hong Guest

    That was the code I have written in my program, sorry I could not post
    all my program because it's quite long. The next light function read new
    line from a file, I have check the function and it works fine. The
    problen happens after the >> call in the read eye part, it does not
    change the value of "command" variable at all.
    Thanks
    Viet
    camera res;


    string command;
    // read image size
    next_line();
    istringstream ss(buffer);
    ss >> command;
    if (command!="IMAGESIZE")
    throw parser_error_exception("Invalid image size command when reading
    camera");
    else
    {
    int width, height;
    ss >> width >> height;
    res.set_image_size(width,height);
    }

    // read eye
    next_line();
    ss.str(buffer);
    ss >> command;
    if (command!="EYE")
    throw parser_error_exception("Invalid eye command when reading camera");
    else
    {
    double x,y,z;
    ss >> x >> y >> z;
    res.set_eye(x,y,z);
    }
    Viet Le Hong, Sep 18, 2003
    #3
  4. Viet Le Hong wrote:
    > That was the code I have written in my program, sorry I could not post
    > all my program because it's quite long. The next light function read new
    > line from a file, I have check the function and it works fine. The
    > problen happens after the >> call in the read eye part, it does not
    > change the value of "command" variable at all.
    > Thanks


    What might have happened, is that the stream object is put into an
    invalid state by one of the read operations. You should generally
    test the state of a stream after every read. The clear () member
    function is used to reset the state.

    The comp.lang.c++ FAQ (Google for C++ FAQ) has a good section on IO
    which is almost certain to help you get your code working.

    Regards,
    Buster.

    > Viet
    > camera res;
    >
    >
    > string command;
    > // read image size
    > next_line();
    > istringstream ss(buffer);
    > ss >> command;
    > if (command!="IMAGESIZE")
    > throw parser_error_exception("Invalid image size command when
    > reading camera");
    > else
    > {
    > int width, height;
    > ss >> width >> height;
    > res.set_image_size(width,height);
    > }
    >
    > // read eye
    > next_line();
    > ss.str(buffer);
    > ss >> command;
    > if (command!="EYE")
    > throw parser_error_exception("Invalid eye command when reading
    > camera");
    > else
    > {
    > double x,y,z;
    > ss >> x >> y >> z;
    > res.set_eye(x,y,z);
    > }
    Buster Copley, Sep 18, 2003
    #4
    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. Donald Canton

    istringstream syntax error question

    Donald Canton, Jan 29, 2004, in forum: C++
    Replies:
    1
    Views:
    399
    David Harmon
    Jan 30, 2004
  2. Luther Baker

    istringstream question

    Luther Baker, May 22, 2004, in forum: C++
    Replies:
    7
    Views:
    3,844
    John Harrison
    May 22, 2004
  3. JustSomeGuy

    A question about istringstream

    JustSomeGuy, Nov 24, 2004, in forum: C++
    Replies:
    6
    Views:
    397
    =?iso-8859-1?Q?Ali_=C7ehreli?=
    Nov 25, 2004
  4. Randy Yates

    istringstream class question

    Randy Yates, Jan 2, 2005, in forum: C++
    Replies:
    8
    Views:
    504
    Jonathan Turkanis
    Jan 3, 2005
  5. Mike Copeland

    istringstream Conversion Question

    Mike Copeland, Jul 31, 2010, in forum: C++
    Replies:
    5
    Views:
    461
    Ian Collins
    Aug 1, 2010
Loading...

Share This Page