c - 从文件填充结构数组

标签 c arrays struct

我有一个充满整数和 double 的 .dat 文件,我需要用它来填充结构数组,但我在弄清楚语法时遇到了问题。

这是文件:

9383      8.86
2777     69.15
7793     83.35
5386      4.92
6649     14.21
2362      0.27
8690      0.59
7763     39.26
540     34.26
9172     57.36
5211     53.68
2567     64.29
5782     15.30
2862     51.23
4067     31.35
3929     98.02
4022     30.58
3069     81.67
1393     84.56
5011     80.42
6229     73.73
4421     49.19
3784     85.37
5198     43.24
8315     43.70
6413     35.26
6091     89.80
9956     18.73
6862     91.70
6996     72.81

这是我的代码:

typedef struct student
{
    double score;
    int id;
    char grades;
} Student;

int getScores(FILE *input, Student class);

void main(void)
{
    char filename[] = "scores.dat";
    FILE *input;
    Student class[MAXNUM];
    int numScores;
    double average;

    input = fopen("scores.dat", "r");

    if (input == NULL)
    {
        printf("EOF");
        exit(1);
    }
}


int getScores(FILE *input, Student class)
{
    double s;
    int i, count = 0;

    while(fscanf(input, "%d %lf", &i, &s) == 2)
    {
        class[count].score = s;
        class[count].id = i;
        count++;
    }

我的主要问题在于我试图用 scores.dat 文件的整数和 double 填充数组的 while 循环。

编译时出现如下错误:

lab5.c:53:8: error: subscripted value is neither array nor pointer nor 
vector
class[count].score = s;
    ^
lab5.c:54:8: error: subscripted value is neither array nor pointer nor 
vector
class[count].id

感谢您提供任何帮助或提示。

最佳答案

只需更改函数 getScores 的定义和声明参数 class 来自

`int getScores(FILE *input, Student class);` 

`int getScores(FILE *input, Student *class);` 

关于c - 从文件填充结构数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55231423/

相关文章:

c - 我正在尝试打印日历,但无法对齐结果

java - 循环填充 JUnit 参数?

javascript - 在 JavaScript 中为每个数组分组、求和并生成一个对象

c - 如何使用指针和数组访问嵌套结构?

pointers - 将结构指针转换为接口(interface){}

dictionary - Go 中复杂键字典的唯一性,但 Julia 中没有?

c - ld-linux.so 执行 CTOR 的顺序?

c - 如何反转十六进制数的字节?

Java 不允许泛型类的内部类数组

c - 数组名称为指针,数组名称带有 & 运算符