【问题标题】:Input to an array of pointers from a file showing error从显示错误的文件中输入指针数组
【发布时间】:2020-05-07 21:18:01
【问题描述】:

对于empF[i],我遇到了这个错误“下标值不是数组、指针或向量”
这是我的代码

employee empF[100];
int i=0;
void test(){
    FILE *fp;
    employee empF;
    fp=fopen("employee.csv","r");
    employee temp;
    char x[100];
    while(fgets(x,100,fp)!=NULL){
        removeCommas(x);
        sscanf(x,"%d %s %ld %s %d",&(temp.employee_id),temp.employee_name,&(temp.phno),temp.shift,&(temp.area_code));
        empF[i].employee_id=temp.employee_id;
        empF[i].employee_name=temp.employee_name;
        empF[i].phno=temp.phno;
        empF[i].shift=temp.shift;
        empF[i].area_code=temp.area_code;
        i+=1;
    }
    fclose(fp);
}

这是员工结构

typedef struct employee
{
    int employee_id;
    char employee_name[20];
    long int  phno;
    char shift[10];
    int area_code;
}employee;

如果我不使用结构数组,它似乎运行得很好。我到底错过了什么?

【问题讨论】:

    标签: c file csv runtime-error structure


    【解决方案1】:

    您在函数中重新定义了变量名empF,并声明为employee(非数组)类型。

    void test(){
        FILE* fp;
        employee empF; /* <-- here */
        /* ... */
    }
    

    所以当您引用empF 时,您引用的是函数内部的非数组局部变量。

    【讨论】:

    • 哦,那是我的错。
    【解决方案2】:

    我猜你忘了删除

    雇员雇员;

    当您从一名员工切换到多名员工时的线路。此声明覆盖先前的数组声明。

    (另外,temp的员工你其实并不需要;你可以直接扫码到empF[i],但你可能有你的理由:))

    【讨论】:

    • 知道了。我的错。
    猜你喜欢
    • 2014-09-10
    • 2021-04-16
    • 2021-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-01
    相关资源
    最近更新 更多