//這里是結構 typedef struct student { int rno; 字符名稱[20];結構主題{ int scode; 字符名稱[20];國際商標;}子[3]; 整數;每浮動;} 學生;
student s1;
FILE *fp;
int j;
fp = fopen("mystudents.txt", "r");
while (fread(&s1, sizeof(student), 1, fp))
{
printf("\n%d \n%s", s1.rno, s1.name);
for (j = 0; j < 3; j )
{
printf("\n%d", s1.sub[j].mark);
}
printf("\n%d", s1.total);
}
fclose(fp);
我的檔案內容:
101 brian 23 45 56 124
102 abhi 32 78 90 200
uj5u.com熱心網友回復:
fread()用于讀取二進制檔案,而不是文本檔案(除非您正在讀取字串變數)。您可以使用fscanf()來決議文本檔案。
student s1;
FILE *fp;
fp = fopen("mystudents.txt", "r");
while (fscanf(fp, "%d %s %d %d %d %d", &s1.rno, s1.name, &s1.sub[0].mark, &s1.sub[1].mark, &s1.sub[2].mark, &s1.total) > 0)
{
printf("\n%d \n%s", s1.rno, s1.name);
for (int j = 0; j < 3; j )
{
printf("\n%d", s1.sub[j].mark);
}
printf("\n%d", s1.total);
}
fclose(fp);
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/462103.html
標籤:C
上一篇:如何完成所有行程,然后使用MPI在C中進行串行作業?
下一篇:你可以在C中按值傳遞字串嗎?
