无法读取我写的文件(C 编程学生数据库)

标签 c

我们接到一项任务,要制作一个程序来读取文本文件中写入的内容(姓名、学号、类(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/

相关文章:

c - 用 "U"指定无符号整数有什么意义?

c++ - 如何使用 OpenLDAP API 选择 LDAP 客户端绑定(bind)到哪个地址?

c - 如何在 C 中运行多个 execlp()?

c - 关于数组下标运算符

从 APC 调用 LdrLoadDll 会导致访问冲突

c - 如何仅使用单词初始化 C 代码

c - 函数atoi返回错误的数字

返回指向特定大小数组的指针的 C 函数声明

c - 如何中断一个IO block ?

c - memset 设置值不正确