Thread: std::stringstream

  1. #1
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    std::stringstream

    Consider this piece of code:
    Code:
    std::string S = "1 2 3";
    std::stringstream SS(S);
    int I;
    SS >> I;
    std::cout << SS.str().c_str();
    After extracting an integer from the stream you will still get the entire stream when calling .str(), meaning "1 2 3". Is there some easy way of retrieving what's left in the stream, in this case "2 3"?
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Here's one way:
    Code:
    std::string remains;
    size_t avail = SS.rdbuf()->in_avail();
        
    // assign(source string, offset, count)
    remains.assign(S, S.length() - avail, avail);
    
    std::cout << "Remaining: \"" << remains << "\"" << endl;
    gg

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    thanks
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. [DirectX] I cant figure out this error...
    By Raigne in forum Game Programming
    Replies: 19
    Last Post: 09-05-2008, 01:50 AM
  2. Four questions on std::stringstream
    By maxhavoc in forum C++ Programming
    Replies: 5
    Last Post: 10-27-2006, 07:02 PM
  3. std::stringstream question
    By Cat in forum C++ Programming
    Replies: 5
    Last Post: 08-26-2006, 12:58 PM