C++;错误:0xC0000005:访问冲突写入位置0xfeeefeee < >我在C++程序中得到这个错误 > 0xC000 00:访问访问写位置0xFEEEEFEE“

C++;错误:0xC0000005:访问冲突写入位置0xfeeefeee < >我在C++程序中得到这个错误 > 0xC000 00:访问访问写位置0xFEEEEFEE“,c++,C++,我的代码是 #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|i

我的代码是

#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;
}
#包括
#包括
#包括
使用名称空间std;
班级员工
{
公众:
字符串名;
智力年龄;
国际电话;
国际工资;
};
int main()
{
员工emp1;
流f1;
f1.打开(“qwe.txt”,ios::binary | ios::app);

对于(int i=0;i您无法以这种方式读/写具有复杂类型的结构,如
std::string
。它们具有特定于实现的内部结构。直接重写其内存是最可靠的方法。请改用
>
操作符:/p precodef12.5岁;
类的内部表示是由实现定义的。此外,
字符串
成员可以直接在成员中保存数据,也可以根据字符数保存在其他堆对象中


这就是为什么需要对类的实例进行序列化。序列化函数将采用序列化目标,如流的
,并写入数据的表示。反序列化函数将采用序列化源,如
ifstream
,并读取对成员的表示。

如果需要低级别代码中使用的el 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;
}
#包括
#包括
#包括
#包括
使用名称空间std;
班级员工{
公众:
字符串名;
智力年龄;
国际电话;
国际工资;
};
int main(){
员工emp1;
流f1;
f1.打开(“qwe.txt”,ios::binary | ios::trunc);
对于(int i=0;i<2;i++)
{
cout>emp1.name;
cout>emp1.0岁;
cout>emp1.phone;
cout>emp1.1工资;

cout
ifstream::read
有一个原型,什么是合同。你违反了这个合同,把演员作为证据。如果你违反了合同,为什么你会期望一些不同于未定义行为的东西?
#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;
}