Get info from string

This is a discussion on Get info from string within the C++ Programming forums, part of the General Programming Boards category; I'm looking to retrieve bits of information from a char string. Basically I get a string with delimiters in a ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    11

    Get info from string

    I'm looking to retrieve bits of information from a char string.

    Basically I get a string with delimiters in a few spots, then I have to get the substrings between each of those delimiters.

    For instance, I have a char string that gets an IP address such as "150.40.230.8". And what I have to do is extract the "150", "40", "230", and "8" in individual elements of a [4] element array of type int.

    Code:
    char ip_address[16] = "";
    
    cout << "Enter an IP: ";
    cin >> ip_address;
    And I want to get those into an array called octet[4]. So in the end I want-
    octet[0] = 150
    octet[1] = 40
    octet[2] = 230
    octet[3] = 8

    I was thinking of using the strtok() function, but wasn't sure exactly how to get it to work. Or perhaps the strchr() function in some way?

    Maybe somehow I could get the position of the first ".", read the stuff before that into octet[0], then from that position find the position of the next "." and read in the stuff between those two positions into octet[1], and so on until I read the last octet where I read from the position of the last "." to the null terminator. Just not sure how the code would look for that.

    Any ideas?

  2. #2
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    Take a look at this thread about strtok().

    That should clear things up for you.
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    11
    Thanks, I think I got it to work, here is the code-

    Code:
        char ip_address[16] = "";
        char *tokptr;
        int octet_count = 0;
    
        cout << "Enter an IP: ";
        cin >> ip_address;
    
        tokptr = strtok( ip_address , "." );
        while( tokptr != NULL )
        {
            octet[octet_count] =  atoi( tokptr );
            tokptr = strtok ( NULL , "." );
            octet_count++;
        };

  4. #4
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    You may be interested in the inet_addr function.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. += operator
    By BKurosawa in forum C++ Programming
    Replies: 8
    Last Post: 08-05-2007, 04:58 AM
  2. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 07:52 PM
  3. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 01:56 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21