// Similar as ex4.c, using an array of struct #include #include //define a structure student typedef struct { char name[20]; int score; } student; int main( void ) { char stfile[] = "stfile", str[20]; int i, n; student st[3]; //declare an array of student FILE *fp; fp = fopen(stfile, "r"); if(fp == NULL) { printf("can not open file %s.\n", stfile); return 1; } printf("\nReading data from the file %s.\n\n", stfile); for(i=0; i<3; i++) { fscanf(fp, "%s %d", &str, &n); strcpy(st[i].name, str); //NOTE: how to use an array of struct st[i].score = n; } printf("st.name st.score\n"); for(i=0; i<3; i++) { printf("%s %d\n", st[i].name, st[i].score); } fclose(fp); return 0; }