Thread: stringstreams problem

  1. #1
    Registered User
    Join Date
    Aug 2006

    Question stringstreams problem

    I'm looking to take an IP address (in the form aaa.bbb.ccc.ddd), which I've stored as a string (m_Address), and convert the octets into four integers (m_A, m_B, m_C, m_D). I parsed the string m_Address into four strings, each containing one of the octets (a_string, b_string, c_string, d_string) and am trying to use stringstreams to convert them to ints. The first conversion works (a_string to m_A), but the rest do not. Successive calls to istringstream::str() seem to fail. I'd rather not make four istringstream objects if I don't have to.

    Also, is there perhaps an easier way to parse the initial string into the four octets? Thanks to all who help.

    int ConvertStringToInt(string m_Address)
        //N.B. Code modified for posting purposes.
        //Error checking, etc. removed for space and clarity.
        //Returns 0 on success, 1 on error.
        int count = 0;
        string a_string = "";
        string b_string = "";
        string c_string = "";
        string d_string = "";
        int m_A, m_B, m_C, m_D;
        for(int i = 0; i < m_Address.length(); ++i)
                if(count == 0)  //aaa
                    a_string += m_Address[i];
                    if(count == 1)  //bbb
                        b_string += m_Address[i];
                        if(count == 2)  //ccc
                            c_string += m_Address[i];
                            if(count == 3)  //ddd
                                d_string += m_Address[i];
            else    //Not a digit.
                if(m_Address[i] == '.')
        //Convert strings to integers.
        istringstream stream_buffer;
        stream_buffer >> m_A;
        stream_buffer >> m_B;
        stream_buffer >> m_C;
        stream_buffer >> m_D;
        /***What I'd like to avoid doing***/
        //m_A = atoi(a_string.c_str());
        //m_B = atoi(b_string.c_str());
        //m_C = atoi(c_string.c_str());
        //m_D = atoi(d_string.c_str());
        return(0);  //Success!    

  2. #2
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Chch, NZ
    you have to clear() the string stream each time.

    however there's a much easier way to do this!
    if m_address is "" (for example) you can simply read it directly out of the stringstream as follows
    int oct1, oct2, oct3, oct4;
    char dot;
    string addr = "";
    stringstream str(addr);
    str >> oct1 >> dot >> oct2 >> dot >> oct3 >> dot >> oct4;
    easy as that.
    Last edited by ChaosEngine; 06-11-2007 at 09:14 PM. Reason: speeling
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  2. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  5. beginner problem
    By The_Nymph in forum C Programming
    Replies: 4
    Last Post: 03-05-2002, 05:46 PM