首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >C++错误: 0xC0000005:访问冲突写入位置0xfeeefeee

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

Stack Overflow用户
提问于 2016-06-16 05:32:56
回答 3查看 772关注 0票数 1

我在程序中得到了错误“0xC0000005:访问冲突写入位置0xfeeefeee"。

我的代码是

代码语言:javascript
运行
复制
#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))可能会造成问题。但我需要这条线。

EN

回答 3

Stack Overflow用户

发布于 2016-06-16 06:11:04

您不能以这种方式读写具有复杂类型的结构,比如std::string。它们有一个具体实施的内部结构。直接覆盖它的记忆是一种最可靠的方式来射击自己的脚。改用>>/<<运算符:

代码语言:javascript
运行
复制
f1 << emp1.name;
f1 << emp1.age;
//...
f2 >> emp2.name;
f2 >> emp2.age;
票数 2
EN

Stack Overflow用户

发布于 2016-06-16 06:12:36

类的内部表示是定义的。此外,string成员可以根据字符的数量直接在成员中或在其他堆对象中保存数据。

这就是为什么需要对类的实例进行序列化。序列化函数将接受像ofstream这样的序列化目标,它会写入数据的表示。反序列化函数将接受像ifstream这样的序列化源,并将代表读入到成员。

票数 1
EN

Stack Overflow用户

发布于 2016-06-16 06:42:12

如果您需要代码中使用的低级别API (读/写),请检查:

请注意,下面的行可能会破坏您的数据,我更改了这一点,以避免以前由您编写的值。

代码语言:javascript
运行
复制
f1.open("qwe.txt", ios::binary | ios::trunc);

完整代码:

代码语言:javascript
运行
复制
#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;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37850582

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档