错误代码:

struct STUD
{
    int ID;//学号
    char name[20];
    float score;
}stud;
STUD SS[10];
student.open("student.dat",ios::in|ios::binary);
    if(!student)
    {
        cout<<"打开文件失败!"<<endl;
        return 0;
    }
    int j=0;
    student.read((char *)&stud,sizeof(stud));
    while(!student.eof()){
        SS[j].name=stud.name;//报错!
        SS[j].ID=stud.ID;
        SS[j].score=stud.score;
        j++;
        student.read((char *)&stud,sizeof(stud));
    }
    student.close();

这段代码的目的是从文件.dat中读取数据,存储到结构体数组对象SS[j]中,但是对SS[j].name赋值是报错,改成了strcpy(SS[j].name,stud.name)即可,这里若用等号需要重载运算符=。注意添加头文件#include<cstring>

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。
扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄