Need help parsing string (homework)

This is a discussion on Need help parsing string (homework) within the C Programming forums, part of the General Programming Boards category; I'm trying to parse the following string and store it in a structure Example String: "01","35083","AL","HOLLY POND",86.617441,34.190085,3838,0.00095 01 - Numeric ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Location
    Seattle
    Posts
    39

    Need help parsing string (homework)

    I'm trying to parse the following string and store it in a structure

    Example String:
    "01","35083","AL","HOLLY POND",86.617441,34.190085,3838,0.00095

    01 - Numeric code (int)
    35083 - Zip code (int)
    AL - State Abbreviation (char array)
    HOLLY POND - City Name (char array)
    86.617441 - Longitude (double)
    34.190085 - Latitude (double)
    3838 - Numeric Population (int)
    0.00095 - Allocation Factor (double)

    Here is what I have so far. My understanding of strtok and strtod kind of break down after the last else if statement.
    Code:
    extern int ZIPS_parse_zips_rec( char * buf, ZIPS_data_p_t zips_rec )
    {
    #define BUFF_SIZE (500)
    	int rcode = TRUE;
    	char *token = NULL;
    	char *inttok = NULL;
    	const char *delims = "\",";
    	char mybuffer[BUFF_SIZE + 1];
    	struct ZIPS_data_tag ZIPS_temp;
    		
    
    	assert( strlen( buf ) < BUFF_SIZE);
    	strncpy(mybuffer, buf, BUFF_SIZE - 1);
    	mybuffer[BUFF_SIZE - 1] = '\000';
    
    	if ( (token = strtok( mybuffer, delims )) == NULL )
    		rcode = FALSE; /* Fail zero tokens */
    	else if ( ZIPS_temp.fips_code = strtoul( token, &inttok, 10 ), *inttok != '\000' )
    		rcode = FALSE; /* Fail invalid int */
    	else if ( (token = strtok( NULL, delims )) == NULL )
    		rcode = FALSE;
    	else if ( ZIPS_temp.zip_code = strtoul( token, &inttok, 10), ZIPS_temp.zip_code < 1 || ZIPS_temp.zip_code > 99/*, *inttok < 1 || *inttok > 99 */)
    	
    		return rcode;
    }

  2. #2
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312
    Why not use sscanf() ?

    sscanf [C++ Reference]
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Yeah. strtok() is better for some things; I don't think this is one of them!

    You will run into a problem with sscanf using %s where there are spaces and because it will grab the "," -- so you have to specify characters in the template:
    Code:
    #include <stdio.h>
    
    struct info {
            int num;
            int zip;
            char state[3];
            char city[64];
            double longt;
            double lat;
            int pop;
            double AF; 
    };
    
    
    int main() {
            struct info eg; 
            char string[]="\"01\",\"35083\",\"AL\",\"HOLLY POND\",86.617441,34.190085,3838,0.00095";
            sscanf(string,"\"%d\",\"%d\",\"%[A-Z]\",\"%[a-zA-Z ]\",%lf,%lf,%d,%lf",
                    &eg.num,&eg.zip,eg.state,eg.city,&eg.longt,&eg.lat,&eg.pop,&eg.AF);
    
            /* check */
            printf("%d\n%d\n%s\n%s\n%lf\n%lf\n%d\n%lf\n",
                    eg.num,eg.zip,eg.state,eg.city,eg.longt,eg.lat,eg.pop,eg.AF);
    
            return 0;
    }
    
    Output:
    1
    35083
    AL
    HOLLY POND
    86.617441
    34.190085
    3838
    0.000950
    Notice the 2nd string specifier is [a-zA-Z ], with a "space" inside the brackets.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312
    You really make everyone's homework :P
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Ideswa View Post
    You really make everyone's homework :P
    It helps me to wake up and avoid my own
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. getopt - parsing string as argument
    By bertazoid in forum C Programming
    Replies: 13
    Last Post: 02-05-2009, 04:35 PM
  2. Replies: 8
    Last Post: 04-25-2008, 03:45 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  4. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 07:49 PM

Tags for this Thread


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