Thread: stringstream with escape characters

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    35

    stringstream with escape characters

    I've been trying to incorporate escape characters into stringstream, but I cannot figure out how to implement this correctly for the life of me. At the moment, I cannot even compile the program because of an "unknown escape sequence" error.

    Code:
    #include <iostream>
    #include <sstream>
    #include <string>
    #include <vector>
    
    using std::string;
    
    int main(void) {
            std::vector<string> tokens;
            string str("This sentence \t should be split up into individual words except for this\ one");
            string buf;
            std::stringstream ss(str);
    
            while (ss >> buf)
                    tokens.push_back(buf);
    
            for (int i = 0; i < tokens.size(); i++) {
                    std::cout << tokens[i] << std::endl;
            }
    }
    Ideally, the output should be:
    This
    sentence
    should
    be
    split
    up
    into
    individual
    words
    except
    for
    this one

    Any assistance would be greatly appreciated.

  2. #2
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    The compiler doesn't understand what to do with the "\ " sequence in the string literal.

    There's no trivially easy way to do what you suggest... Boost has a string tokenizer that would allow you to treat anything within double quotes as a single token.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. Replies: 10
    Last Post: 07-10-2008, 03:45 PM
  3. How do you check how many characters a user has entered?
    By engstudent363 in forum C Programming
    Replies: 5
    Last Post: 04-08-2008, 06:05 AM
  4. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  5. Escape characters
    By The Gweech in forum C++ Programming
    Replies: 3
    Last Post: 07-09-2002, 05:38 PM