Thread: std::stringstream question

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    1,619

    std::stringstream question

    What is the easiest way to tell if an extraction from a stringstream parsed all the stream, or only part of it?

    E.g. I want to use this for, say, string -> int conversion. If I have an input, "-123", then it should parse the entire string and return -123. If I have the input "-123abc", it also returns "-123" but I want to make this an error condition (as the input string was not a properly formatted int) rather than silently parsing only half of the input string and returning.

    The reason I am using stringstream is firstly as I rarely use it and want the practice, secondly because I can easily make this a function template and be able to extract any kind of data that std::stringstream can work with.
    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.

  2. #2
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    If I understood your question, one way would be to have a templated class that would both do the parsing and store the results. I'm not comfortable around templates yet. So, instead of showing you the actual class skeleton (which you can probably do better than me), maybe show you its use...

    Code:
    istringstream myText;
    
    /*...*/
    
    strParse<int> result(myText);
    
    result.value();  // outputs value
    result.error();  // probably returns an int
    result.desc();  // error description
    Or if you are going to work with just one datatype, ignore the template
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    I think I solved it, the following is my code:

    Code:
    template <typename Type>
    	Type FromString(tstring str){
    		
    		Type t;
    		std::basic_stringstream<TCHAR> s(str);
    		s >> t;
    
    		if ((int) s.tellg() != (int) str.size())
    			throw std::runtime_error("Extraction Failed");
    		return t;
    	}
    Basically I am checking if the number of bytes parsed is different from the length of the input string.


    I think your way should work, too, Mario. I may recode this as a templated class instead of a templated function.
    Last edited by Cat; 08-25-2006 at 09:38 PM.
    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.

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Ok... I hadn't understood your question. Now I do
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    Code:
    template<class T>
    bool parse(T &e, const std::string &s) {
        std::istringstream iss(s);
        return (iss >> e) && iss.eof();
    // or, to accept trailing whitespace
        return (iss >> e >> std::ws) && iss.eof();
    }
    you can look to boost::lexical_cast for a more thourough example.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM