错误代码:
struct STUD
{
int ID;//学号
char name[];
float score;
}stud;
STUD SS[];
student.open("student.dat",ios::in|ios::binary);
if(!student)
{
cout<<"打开文件失败!"<<endl;
return ;
}
int j=;
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>