Tokenizing strings to long integers and doubles

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • arnaudk
    Contributor
    • Sep 2007
    • 425

    Tokenizing strings to long integers and doubles

    Hello,

    I need to get the following string

    "12332321213,So meText,3.141592 654"

    into constituent variables: an unsigned long int, a string and a double. So far I have,
    Code:
    string line = "12332321213,SomeText,3.141592654";
    unsigned long int id; // should become 12332321213
    string name; // should become "SomeText"
    double number; // should become 3.141592654
    
    vector <string> lineTokens;
    istringstream iss(line) // Create string stream
    
    string token;
    while (getline(iss,token,',')) {
       lineTokens.push_back(token);
    }
    Now I have a vector of strings. So the question arises: how do I convert the string "1233232121 3" to a long int and "3.14159265 4" to a double? Of course, atoi() comes to mind but it looks like it only converts to single precision integers.

    I tried static_cast<uns igned long int>() but I get the compile-time error " invalid static_cast from type `std::string' to type `long unsigned int' ".

    (An irrelevant aside: how do I specify what kind of code I'm posting in this forum because mine comes up as 'text' and not 'cpp'?)
  • arnaudk
    Contributor
    • Sep 2007
    • 425

    #2
    D'oh! Got it: strtoul(); and strtod();
    [blush]

    Comment

    • Ganon11
      Recognized Expert Specialist
      • Oct 2006
      • 3651

      #3
      As an answer to your [CODE] tag related question, there is an option to include a "=<code type here>" between the word CODE and the closing bracket. For instance, the following
      Code:
       segment uses =cpp:
      
      [code=cpp]
      cout << "Now you know this is C++ code!" << endl;
      There's also =java, =c, =php, etc. etc.

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by arnaudk
        D'oh! Got it: strtoul(); and strtod();
        [blush]
        You forgot the [/blush] tag, or are you still blushing ;-)

        kind regards,

        Jos

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          Originally posted by arnaudk
          D'oh! Got it: strtoul(); and strtod();
          [blush]
          Yes. Providing you write in C. But this is C++.

          You have:

          An int. A comma. A string. A comma, A double.

          Originally posted by arnaudk
          string line = "12332321213,So meText,3.141592 654";
          So, use a stringstream:

          [code=cpp]
          stringstream ss;
          ss << line;
          int AnInt;
          char AChar;
          string AString;
          double ADouble;
          ss >> AnInt >>AChar >>AString>>ACha r>>ADouble;
          [/code]

          and you are done.

          And stop using relic C functions from the old C string library. C++ strings are no C-style strings.

          Comment

          • arnaudk
            Contributor
            • Sep 2007
            • 425

            #6
            Originally posted by weaknessforcats

            You have:

            An int. A comma. A string. A comma, A double.

            So, use a stringstream:

            [code=cpp]
            stringstream ss;
            ss << line;
            int AnInt;
            char AChar;
            string AString;
            double ADouble;
            ss >> AnInt >>AChar >>AString>>ACha r>>ADouble;
            [/code]
            Thanks, I'd to bury my torturous C past. But in your code, how does ss know that the delimiter is a comma? Is that default behavior? What if my delimiter were something else?

            Comment

            • Ganon11
              Recognized Expert Specialist
              • Oct 2006
              • 3651

              #7
              I think that the ss detects what kind of variable it is being asked for and spits out what is necessary. It is first asked for an integer, and so it returns the integer it has in front. Then it is asked for a single char. Well, the next char is the comma, so it spits that out.

              There might be trouble, though, asking it for a string and then a char, because a comma could be part of a string.

              Comment

              • arnaudk
                Contributor
                • Sep 2007
                • 425

                #8
                Apparently, for numeric values, the >> operator

                "Extracts characters from the input sequence and tries to interpret them as a numeric value of the given parameter's type. If successful the value is stored in val. The specific way in which the data is parsed depends on the manipulators previously used on the stream and on its associated locale."

                For strings, however, it

                "Extracts characters and stores them as a c-string (i.e. in succesive locations starting at location pointed by str and terminated by a null-character). Extraction ends when the next character is either a valid whitespace or a null character, or if the End-Of-File is reached.The terminating null character is automatically appended after the extracted characters.
                The extraction operation can be limited to a certain number of characters (thus avoiding the possibility of buffer overflow) if the field width (which can be set with ios_base::width or setw) is set to a value greater than zero. In this case, the extraction ends one character before the count of characters extracted reaches the value of field width, leaving space for the ending null character. After a call to this extraction operation the value of the field width is automatically reset to zero."

                (from cplusplus.com)

                [/blush]

                Comment

                • weaknessforcats
                  Recognized Expert Expert
                  • Mar 2007
                  • 9214

                  #9
                  Originally posted by arnaudk
                  Thanks, I'd to bury my torturous C past. But in your code, how does ss know that the delimiter is a comma? Is that default behavior? What if my delimiter were something else?
                  It's not ss.

                  It's the operator>> function, which may or may not be part of the stringstgream class.

                  Each operator>> overload has two operators. The first operator is the input stream and the second operator is the variable to the right of the >> operator.

                  So, >> aChar assumes there is a char as the next byte in the inout stream. If you don't know that, then you can't use the >> operator at all.

                  >> operations are all formatted operations since they presuppose you know the format of the data in the inout stream.

                  Comment

                  Working...