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

string is a C++ type. You'll want to convert your code to use C++ instead. string.h is a C header file and doesn't provide that.
If you don't want to use C++, you should use character arrays. And instead of acountry[j]=arr; you would use strcpy(&acountry[j].code,&arr);

You're right. I was confusing things. Subbing it for a char array worked. I still have an issue, but my main question here was solved (and besides, I've reworked a lot of it anyway).

Edited 4 Years Ago by shaunchu: n/a

This question has already been answered. Start a new discussion instead.