Thread: istringstream

  1. #1
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681

    istringstream

    For reference: http://cboard.cprogramming.com/showthread.php?t=53945

    Well since using stringstream was mentioned I decided to take a look into it. For outputing the stuff its wonderful. A template function and an overloaded function and everything is covered.
    As usual outputting seems to be 100 easier then inputting.

    The problem I'm having is getting the characters that have been used removed from the string.
    Code:
    #include <sstream>
    #include <iostream>
    #include <string>
    
    int main()
    {
      using namespace std;
    
      string s = "134729l192.23839";
    
      istringstream str(s);
    
      int x, z;
      char y;
    
      str>>x;
      str>>y;
      str>>z;
      cout<<x<<endl<<y<<endl<<z<<endl;
    //Not sure what to do here to get the following to display ".23839"
      cout<<s<<endl;
    }
    I tried str.str() which should return the stream's internal buffer per http://www.cplusplus.com/ref/iostrea...tream/str.html but I always get back nothing.

    From the testing I've done and the reading I've done it appears that it has the some "frailties" as cin (which I guess I should expect since stringstreams are deivered from the same base).

    Also another quicky question to confirm my observation. Once
    Code:
      string s = "134729l192.23839";
      istringstream str(s);
    any changes made to s do not affect str until sync() is called correct?

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >//Not sure what to do here to get the following to display ".23839"
    I don't suppose that
    Code:
    cout<< y << z <<endl;
    Would cut it?
    My best code is written with the delete key.

  3. #3
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    If only

    For this to work I need the string to remove the characters used by the stream.

  4. #4
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    so you mean you want s to just be that end part ".23839"?

    Could you find the length of x and then shift the s string over that many spaces to chop off the first part?

  5. #5
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    I want it to behave pretty much like an input buffer. the string would be the buffer. So as the stringstream uses characters I want them removed from the string. If I have to calculate the number of characters to convert the int I might as well do the whole conversion myself and save myself the headache

    Now the removal doesn't have to be done as the characters are being used but if there is some way for me to get the number of characters used in the last process that would be good enough.

  6. #6
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Maybe put the leftovers into a string and output that?
    Code:
    #include <sstream>
    #include <iostream>
    #include <string>
    
    int main()
    {
      using namespace std;
    
      string s = "134729l192.23839";
    
      istringstream str(s);
    
      int x, z;
      char y;
      string leftovers;
    
      str>>x;
      str>>y;
      str>>z;
      cout<<x<<endl<<y<<endl<<z<<endl;
    //Not sure what to do here to get the following to display ".23839"
      str >> leftovers;
      cout<<leftovers<<endl;
    }

  7. #7
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    omg I can't believe I didn't see such an arse solution. No real need for another string as
    Code:
     str>>s;
    works just fine. Thanks jlou

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. istringstream problem in switch block?
    By wolfindark in forum C++ Programming
    Replies: 4
    Last Post: 06-24-2007, 11:56 PM
  2. Replies: 1
    Last Post: 10-27-2006, 01:21 PM
  3. Initializing an istringstream
    By cunnus88 in forum C++ Programming
    Replies: 2
    Last Post: 05-11-2006, 08:49 PM
  4. confused with istringstream
    By terracota in forum C++ Programming
    Replies: 4
    Last Post: 07-23-2004, 10:49 PM
  5. Istringstream fail
    By pianorain in forum C++ Programming
    Replies: 2
    Last Post: 03-09-2003, 11:28 PM