繁体   English   中英

C ++错误:0xC0000005:访问冲突写入位置0xfeeefeee

[英]C++ ERROR: 0xC0000005: Access violation writing location 0xfeeefeee

我在c ++程序中收到此错误“0xC0000005:访问冲突写入位置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))可能会产生问题。 但我需要这条线。

您无法读取/编写具有复杂类型的结构,例如std::string 它们具有特定于实现的内部结构。 直接覆盖它的记忆是一种可靠的射击方式。 使用>> / <<运算符代替:

f1 << emp1.name;
f1 << emp1.age;
//...
f2 >> emp2.name;
f2 >> emp2.age;

类的内部表示是实现定义的。 另外, string成员可以根据string的数量直接或在另外的堆对象中保存成员中的数据。

这就是为什么你需要对你的类的实例进行序列化的原因。 该serialize函数将序列化对象像ofstream一个中的数据写入的represenation。 反序列化函数将采用ifstream等序列化源,并向成员读取reprensentation。

如果您需要代码中使用的低级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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM