我们接到一项任务,要制作一个程序来读取文本文件中写入的内容(姓名、学号、类(class)、年份、部门等。等等。)但我似乎无法让它工作,你能告诉我出了什么问题吗?
#include <windows.h>
#include <conio.h>
#include <stdio.h>
struct record
{
char name[50],number[50],course[50],gender;
int year, section;
float midterm,final,average,sum;
};
int main()
{
int a=1,n;
int passed=0,failed=0;
FILE *fp;
fp = fopen("BSU.txt","r");
if(fp==NULL)
{
printf("ERROR!");
getch();
}
struct record student[25];
printf("Please input the number of students: ");
scanf("%d", &n);
for(a=0;a<n;a++)
{
fscanf(fp, "%f", student[a].average);// I CANNOT MAKE THE FSCANF WORK!!//
}
getch();
}
最佳答案
应该是
fscanf(fp, "%f", &student[a].average);
而不是
fscanf(fp, "%f", student[a].average);
但这只允许您读取包含数字的文件,例如:
1.5
1.9
2.7
您要读取的文件比较复杂。
因此,在您的 for
循环中,您需要读取 10 行,从每行中提取相关信息,将该信息存储在记录的相应字段中,并将记录存储在某处。
关于无法读取我写的文件(C 编程学生数据库),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35743051/