I'm trying to read in a file of text and store it inside an array using a C function, but running into pointer difficulties:
The file to be read is in the format \t:
France Baguette
China Rice
Japan Sushi
etc.
My code is the following, but currently only shows the last entry in the file:
void func(char *a[])
{
FILE *dic = fopen("dic.log", "r");
char *country = malloc(30);
char *food = malloc(30);
int i = 0;
while (fscanf(dic, "%s %s", country, food) == 2)
{
a[i] = country;
a[i+1] = food;
i+=2;
}
fclose(dic);
}
int main (int argc, char*argv)
{
char *b[6];
func(b);
printf("%s %s\n", b[0], b[1]);
printf("%s %s\n", b[2], b[3]);
return 1;
}
I want the function to populate array 'b' in the following manner:
b[france,baguette,china,rice,japan,sushi]