Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在文件中读取/写入自定义对象的向量_C++_File_Vector - Fatal编程技术网

C++ 在文件中读取/写入自定义对象的向量

C++ 在文件中读取/写入自定义对象的向量,c++,file,vector,C++,File,Vector,我正在编写代码,将“person”对象的向量写入一个文件,然后读取该文件。我得到了正确的输出,但在main()函数的末尾,我得到了错误“未处理的异常写入位置……” 我做错了什么,如何调试这样的错误? 这是我的密码: #include <iostream> #include <fstream> #include <string> #include <vector> using namespace std; class person{ public:

我正在编写代码,将“person”对象的向量写入一个文件,然后读取该文件。我得到了正确的输出,但在main()函数的末尾,我得到了错误“未处理的异常写入位置……” 我做错了什么,如何调试这样的错误? 这是我的密码:

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;

class person{
public:
    string name;
    int age;


 person(){

 }
 person(string name,int age){
     this->name= name;
     this->age = age;

 }

  virtual void display(){

       cout << name <<" "<< age << endl;
  }

};



int main () {

fstream f;
f.open("amu.txt",ios::in | ios::out | ios::trunc);
if(f){
    cout<<"file found"<<endl;
}
else{
    cout<<"file not found"<<endl;
    terminate();
}

// creating a vector
vector<person> per;

//populating with "person" objects

for(int i=0;i<100;i=i+10)
{
    person amu("ameykamat",i);
    per.push_back(amu);
}

//wrting objects from vector to the file

for(vector<person>::iterator itr = per.begin(); itr != per.end() ; ++itr)
    {

    f.write((char *)&(*itr),sizeof(*itr));

    }

// taking curson to the start of the file

    f.seekg(0,ios::beg);

//creating temp obj for printing from file
    person amu;
    while(f.read((char *)&amu, sizeof(amu))){
    cout<<(amu.name).c_str()<<" "<<(amu.age)<<endl;
}


   f.close();

system("pause");

  return 0;
}
#包括
#包括
#包括
#包括
使用名称空间std;
班主任{
公众:
字符串名;
智力年龄;
人(){
}
person(字符串名称,整数年龄){
此->名称=名称;
这个->年龄=年龄;
}
虚拟空显示(){

coutA
std::string
如果不是一个简单的
char[]
:没有任何证据表明,当您从其地址复制长度为sizeof(std::string)的数据时,您实际上复制了数据。标准实现使用动态分配,因此您几乎可以确定不会以这种方式复制实际数据

你应该想象一个序列化格式(例如:AGE、名称长度、NoLead终止字符数组的名称),但是你也应该在C++ FAQ中查找引用。 如果它只在一台机器(或同一类型的机器)和同一个编译器(即没有架构更改问题)上运行,则可以通过这种方式实现:

class person {
   ...
    ostream& write(ostream& os) {
        int l = name.length();
        os.write((const char *) &age, sizeof(age));
        os.write((const char *) &l, sizeof(size_t));
        os.write(name.c_str(), name.length());
        return os;
    }

    istream& read(istream& is) {
        int l;
        is.read((char *) &age, sizeof(age));
        if(! is.eof()) {
            is.read((char *) &l, sizeof(size_t));
            if (! is.eof()) {
                char* n = new char[l];
                is.read(n, l);
                name.assign(n,l);
                delete[] n;
            }
        }
        return is;
    }
}
然后使用:

for(vector<person>::iterator itr = per.begin(); itr != per.end() ; ++itr)
    {
    itr->write(f);
    }
...
while(! amu.read(f).eof()){
    cout<<(amu.name).c_str()<<" "<<(amu.age)<<endl;
}
for(vector::iterator itr=per.begin();itr!=per.end();++itr)
{
itr->write(f);
}
...
而(!amu.read(f).eof()){

cout
person
不是简单的可复制的,这无法工作。“简单的可复制”是什么意思?
f.write((char*)&(*itr),sizeof(*itr));
基本上被破坏了,因为每个
person
对象中的
std::string name
成员可能包含指向动态分配(“heap”)的指针实际存储文本内容的内存。保存指针值,但如果从另一个进程(甚至是同一程序的另一个实例/运行)重新加载指针值,字符串内容将不在这些虚拟地址。将文本本身保存到文件ala:
f age amu.age&&std::getline(f,amu.name))…使用amu…`。