So, I'm doing a test of strtok and my csv reader to fill an array of structs. Each line in the csv has a bunch of country information, and I'm only pulling the 2nd, 3rd, 8th, and 9th fields in each line to make the structs.
My biggest problem at the moment is an error stating: "expected specifier-qualifier-list before string" from within my struct. What's wrong with my struct declaration?
I'm pretty sure this code works, as I've tested it on a small file without the structs, but let me know if I've mucked it up.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXFLDS 2000 /* maximum possible number of fields */
#define MAXFLDSIZE 3200 /* longest possible field */
typedef struct{string code; string name; string pop; string life;} country;
void parse( char *record, char *delim, char arr[][MAXFLDSIZE],int *fldcnt)
{
char*p=strtok(record,delim);
int fld=0;
while(p)
{
strcpy(arr[fld],p);
fld++;
p=strtok('\0',delim);
}
*fldcnt=fld;
}
int main(int argc, char *argv[])
{
country acountry[500];
char tmp[1024]={0x0};
int fldcnt=0;
char arr[MAXFLDS][MAXFLDSIZE]={0x0};
int recordcnt=0;
FILE *in=fopen("All.csv","r"); /* open file on command line */
while(fgets(tmp,sizeof(tmp),in)!=0) /* read a record */
{
int i=0;
int j=0;
recordcnt++;
printf("Country number: %d\n",recordcnt);
parse(tmp,",",arr,&fldcnt); /* whack record into fields */
for(i=0;i<fldcnt;i++)
{ /* print each field */
if(i=1)
acountry[j].code = arr[i];
if(i=2)
acountry[j].name = arr[i];
if(i=7)
acountry[j].pop = arr[i];
if(i-8)
{
acountry[j].life = arr[i];
j++;
}
//printf("\tField number: %3d==%s\n",i,arr[i]);
}
}
fclose(in);
return 0;
}