C ++:读写班 [英] C++: Read and write classes

查看:204
本文介绍了C ++:读写班的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>

using namespace std;


class PERSON{
    string name, surname;

public:

    void set_name(string aname, string asurname){
        name = aname;
        surname = asurname;
    };

    void read(){

    }

    void output(){
     cout << name << " " << surname << "\n";
    }

    void save(PERSON *x){
        ofstream file("test.bin", ios::out | ios::app | ios::binary);


        if(!file.is_open()){
                cout << "ERROR\n";
        }else{
                file.write((char*)x, sizeof(*x));
                file.close();
        }

    }
};




/*
 * 
 * 
 * 
 */
int main(int argc, char** argv) {




    PERSON * person1 = new PERSON;
    PERSON * person2 = new PERSON;

    person1->set_name("Amon", "Raa");
    person1->save(oseba1);








    ifstream file2("test.bin", ios::in | ios::binary);

    if(!file2.is_open()){
        cout << "Error\n";
        return 0;
    }


    while(!file2.eof()){
        file2.read((char *)person2, sizeof(*person2));
        person2->output();
    }

    file2.close(); 


    return 0;


}

这是我的code ......我在做什么错了?
我所试图做的是每次类保存到二进制文件的末尾,然后读取所有条目...

This is my code...what I am doing wrong? What I am trying to do is to save each time a class to the end of the binary file and then read all entries...

但每次我运行程序时我得到印只有最后输入的名称

but each time I run the program I get printed only the last entered name

所以运行它第一次
该文件被正确地写入,输出正常
然后,我将名称更改为别的东西,可以说李四,我得到2倍的输出李四

so run it first time the file is written correctly and the output is OK then I change the name to something else, lets say John Doe, I get the output of 2times John Doe

请帮忙......我是一个完整的初学者;(

Please help... I am a complete beginner ;(

推荐答案

您不能简单地写出来在C ++类的二进制图像,尤其是一个包含指针和非POD成员。一旦你在你的数据具有间接性,简单地写了一个内存映像不工作,一方面是因为刚写出类的内存映像不包括所指向的数据,并因为为了这个工作,你会必须加载所有的数据,包括所指向的数据转化为他们在当你保存它们完全相同的内存位置。这不是容易做到(说得客气一点)。

You can't simply write out the binary image of a class in C++, especially one that contains pointers and non-POD members. Once you have indirections in your data, simply writing out a memory image doesn't work, both because just writing out the memory image of the class doesn't include the pointed-to data and because in order for this to work you'd have to load all the data including the pointed-to data into the exact same memory location that they were in when you saved them. That's not easily possible (to put it mildly).

您有两种选择,一是手动,一个使用第三方库:

You have two options, one manual, one using a third party library:

1)你写的和一堆记录信息的读取每个成员单独出来。这应该在你的情况下,因为所有的工作,你真的要加载和保存是两个字符串的内容和它们各自的长度

1) You write and read each member out separately with a bunch of bookkeeping information. That should work in you case as all you really have to load and save is the contents of the two strings and their respective lengths

2)另一种选择 - 尤其是当数据结构是不是您正在使用一个更复杂 - 就是使用类似的的boost ::系列化做繁重的工作适合你。

2) The other option - especially when the data structure is more complex than the one you are using - is to use something like boost::serialization to do the grunt work for you.

这篇关于C ++:读写班的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆