我收到此错误" 0xC0000005: c ++ 程序中的访问冲突写入位置0xfeeefeee" 。
我的代码是
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
class Employee
{
public:
string name;
int age;
int phone;
int salary;
};
int main()
{
Employee emp1;
ofstream f1;
f1.open("qwe.txt",ios::binary|ios::app);
for(int i=0;i<2;i++)
{
cout<<"enter name:\t";
cin>>emp1.name;
cout<<"enter age:\t";
cin>>emp1.age;
cout<<"enter phone:\t";
cin>>emp1.phone;
cout<<"enter salary:\t";
cin>>emp1.salary;
cout<<"\n";
f1.write((char *)(&emp1),sizeof(Employee));
}
f1.close();
Employee emp2;
ifstream f2;
f2.open("qwe.txt",ios::binary|ios::in);
while(f2)
{
f2.read((char *)(&emp2),sizeof(Employee));
if(f2.eof())
{
break;
}
else
{
cout<<"\n"<<emp2.name;
cout<<"\n"<<emp2.age;
cout<<"\n"<<emp2.phone;
cout<<"\n"<<emp2.salary<<"\n";
}
}
f2.close();
cin.get();
return 0;
}
我认为问题出在while(f2)
。但我不确定。这一行f2.read((char *)(&emp2),sizeof(Employee))
可能会产生问题。但我需要这一行。
答案 0 :(得分:2)
您无法以这种方式读取/编写具有复杂类型的结构,例如std::string
。它们具有特定于实现的内部结构。直接覆盖它的记忆是一种可靠的射击方式。请改为使用>>
/ <<
运营商:
f1 << emp1.name;
f1 << emp1.age;
//...
f2 >> emp2.name;
f2 >> emp2.age;
答案 1 :(得分:1)
类的内部表示是实现定义的。此外,string
成员可以直接或在另一个堆对象中保存数据,具体取决于字符数。
这就是为什么你需要对你的类的实例进行序列化的原因。序列化函数将采用类似ofstream
的序列化目标,并写入数据的表示。反序列化函数将采用类似ifstream
的序列化源,并向成员读取重新表示。
答案 2 :(得分:0)
如果您需要代码中使用的低级API(读/写),请查看:
请注意,以下行可能会破坏您的数据,我对此进行了更改,以避免您之前的估价值。
f1.open("qwe.txt", ios::binary | ios::trunc);
完整代码:
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
using namespace std;
class Employee {
public:
string name;
int age;
int phone;
int salary;
};
int main() {
Employee emp1;
ofstream f1;
f1.open("qwe.txt", ios::binary | ios::trunc);
for (int i = 0; i < 2; i++)
{
cout << "enter name:\t";
cin >> emp1.name;
cout << "enter age:\t";
cin >> emp1.age;
cout << "enter phone:\t";
cin >> emp1.phone;
cout << "enter salary:\t";
cin >> emp1.salary;
cout << "\n";
size_t lenght = emp1.name.size();
char lenghtval[sizeof(lenght)];
std::memcpy(&lenghtval, &lenght, sizeof(lenght));
f1.write(lenghtval, sizeof(lenght));
const char *name = emp1.name.c_str();
f1.write(name, static_cast<int>(lenght));
int val = emp1.age;
char towrite[sizeof(val)];
std::memcpy(&towrite, &val, sizeof(val));
f1.write(towrite, sizeof(val));
val = emp1.phone;
std::memcpy(&towrite, &val, sizeof(val));
f1.write(towrite, sizeof(val));
val = emp1.salary;
std::memcpy(&towrite, &val, sizeof(val));
f1.write(towrite, sizeof(val));
}
f1.close();
Employee emp2;
ifstream f2;
f2.open("qwe.txt", ios::binary | ios::in);
while (f2) {
size_t lenght = 0;
char lenghtval[sizeof(lenght)];
f2.read(lenghtval, sizeof(lenght));
std::memcpy(&lenght, lenghtval, sizeof(lenght));
char name[lenght + 1];
f2.read(name, static_cast<int>(lenght));
name[lenght] = '\0';
emp2.name = name;
int val = 0;
char toread[sizeof(val)];
f2.read(toread, sizeof(val));
std::memcpy(&val, toread, sizeof(val));
emp2.age = val;
f2.read(toread, sizeof(val));
std::memcpy(&val, toread, sizeof(val));
emp2.phone = val;
f2.read(toread, sizeof(val));
std::memcpy(&val, toread, sizeof(val));
emp2.salary = val;
if (f2.eof()) {
break;
}
cout << "\n" << emp2.name << std::endl;
cout << "\n" << emp2.age;
cout << "\n" << emp2.phone;
cout << "\n" << emp2.salary << "\n";
}
f2.close();
cin.get();
return 0;
}