Im trying to read a file with about 200 lines, and thereafter saving the diffrent lines into a struct. The stuct is as following:
struct person_data{
char name[20];
char lastname[30];
int age;
char country[3]
};
The lines in the file is setup as:
"Tom HALL" 32 ENG
"Ryan SMITH" 24 USA
So my problems is that i am not sure how i am supposed to read that file, and put the information into a struct. I how been trying a to save the file into my struct, and print it, but the result is a directory paths on my computer and a lot of weird symbols before it crashes. My code is:
int main(){
FILE *fp;
fp = fopen("person.txt", "r");
person_data *person = malloc(sizeof(struct person_info));
int i = 0;
if(fp != NULL) {
while ( i < 200 ) {
fscanf(fp, "%[A-Za-z] %[A-Z] %d %[A-Z]",
person[i].name,
person[i].lastname,
&person[i].age,
person[i].country);
i++;
}
}
else{
perror(fp);
}
for (i = 0; i < 200; i++) {
printf("%s %s %d %s",
person[i].name,
person[i].lastname,
person[i].age,
person[i].country);
}
fclose(fp);
return 0;
}
I am not sure what went wrong, and would therefore like to ask if anybody knows what i did wrong and how to fix this. When i run the program it looks like this: