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;
}