所需的输出:
Student 99704 exam1 97 exam2 64 exam3 61 grade C
但是得到:
Student 991704,97,64,61,C exam1 0 exam2 4195335 exam3 &d ((null)) on class.
打印数组可能需要for
循环吗?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void flushScanf();
int main(void)
{
#if 0
char str[10];
printf("Enter a string: ");
scanf("%9[^\n]", str);
flushScanf();
printf("%s", str);
#endif
FILE* f = fopen("grades.csv", "r");
if (f == NULL) {
fprintf(stderr, "Error opening file test.dat.");
exit(1);
}
///////
char line[65];
char studentID[26];
char grade[8];
int exam1, exam2, exam3;
while (fgets(line, sizeof(line), f) != NULL){
sscanf(line,
"%25[^\n], %*s, %d, %d, %d, %s",
studentID, &exam1, &exam2, &exam3, grade);
printf("Student %s exam1 %d exam2 %d exam3 &d (%s) on class.\n",
studentID, exam1, exam2, exam3, grade);
}
if (fclose(f) == EOF){
fprintf(stderr, "Error closing file test.dat.");
exit(2);
}
return 0;
}
void flushScanf()
{
while (getchar() != '\n')
;
}