The main problem is you're trying to strtok() the same string multiple times.
> ports->LocalID = getfield(tmp,2);
> ports->siteNumber = getfield(tmp, 1);
> ports->fieldName = getfield(tmp, 3);
The problem is at the 2nd call, all the strtok calls have completely trashed your tmp string.
Examine this
Code:
char *tmp=strdup(line);
printf("Get 2nd field from string %s\n", tmp);
ports->LocalID = getfield(tmp,2);
printf("Get 1st field from string %s\n", tmp);
ports->siteNumber = getfield(tmp, 1);
printf("Get 3rd field from string %s\n", tmp);
ports->fieldName = getfield(tmp, 3);
> The image “http://i.imgur.com/1eK5uqW.png” cannot be displayed, because it contains errors.
FWIW, start simple.
Code:
typedef struct airPdata{
char siteNumber[100]; //FAA Site Number
char LocalID[100]; //Airport's "Short Name", ie MCO
char fieldName[100]; //Airport Name
char city[100]; //Associated City
char state[100]; //State
char latitude[100]; //Latitude
char longitude[100]; //Longitude
char controlTower[100]; //Control Tower (Y/N)
}airPdata;
You can choose more reasonable numbers later, or use malloc/strdup with pointers, once everything else is working.
Try
Code:
char line[BUFSIZ];
airPdata port;
while ( fgets(line,sizeof(line),fp) != NULL ) {
if ( sscanf(line,"%[^,],%[^,],%[^,],",
port.siteNumber, port.LocalID, port.fieldName) == 3 ) {
printf("success\n");
}
}