【发布时间】:2014-12-16 19:44:08
【问题描述】:
以下是我试图理解的程序。我唯一感到困惑的部分是 while fscanf(....) == 4 的 while 语句以及 if(...) == 0 的部分。
有人可以向我解释一下这条线及其用途吗?
#include <stdio.h>
#include <stdlib.h>
struct str_student {
char UFID[9];
char firstname[20];
char major[10];
int age;
};
int main(int argc, char *argv[]) {
FILE *fStud = fopen("students.dat", "r");
struct str_student S[11];
int i, n = 0;
while( fscanf(fStud, "%s %s %s %i", S[n].UFID, S[n].firstname, S[n].major, &S[n].age) == 4)
{ if(( S[n].age > 40 ) && ( strcmp(S[n].major, "ECE") == 0 ))
n = n + 1;
}
printf("\nStudents of the ECE Department who are 41 or more years old:\n");
for( i=0; i<n; i++ ) {
printf("%s\n", S[i].UFID);
}
return 0;
}
【问题讨论】:
-
您可能想阅读例如this
fscanfreference,看看它返回了什么。还可以在该网站上搜索其他功能 (strcmp) 以获取更多信息。