源码:
void importData()
{
student s;
ifstream infile;
infile.open("D:/CPP/data",ios::in);
if(infile)
{
// infile.read((char*)&s, sizeof(student));
infile.read(reinterpret_cast<char*>(&s), sizeof(student));
cout<<s.gender;
cout<<s.id;
cout<<s.name;
cout<<s.age;
infile.close();
}
else cout<<"读取失败,请重试";
infile.close();
}
void outData()
{
ofstream outfile;
outfile.open("D:/CPP/data",ios::out);
student s;
s.age=74;
s.name="hello";
s.id=123;
s.gender="男";
if(outfile)
{
outfile.write((char*)&s,sizeof(student));
outfile.close();
}
else cout<<"读取失败,请重试";
outfile.close();
}
写入正常,读取时可以读到但是会报错
0x7afbad54 (msvcp100d.dll) 处有未经处理的异常: 0xC0000005: 写入位置 0xfeeefeee 时发生访问冲突
求大神指导
void importData()
{
student s;
ifstream infile;
infile.open("D:/CPP/data",ios::in);
if(infile)
{
// infile.read((char*)&s, sizeof(student));
infile.read(reinterpret_cast<char*>(&s), sizeof(student));
cout<<s.gender;
cout<<s.id;
cout<<s.name;
cout<<s.age;
infile.close();
}
else cout<<"读取失败,请重试";
infile.close();
}
void outData()
{
ofstream outfile;
outfile.open("D:/CPP/data",ios::out);
student s;
s.age=74;
s.name="hello";
s.id=123;
s.gender="男";
if(outfile)
{
outfile.write((char*)&s,sizeof(student));
outfile.close();
}
else cout<<"读取失败,请重试";
outfile.close();
}
写入正常,读取时可以读到但是会报错
0x7afbad54 (msvcp100d.dll) 处有未经处理的异常: 0xC0000005: 写入位置 0xfeeefeee 时发生访问冲突
求大神指导