1
#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;


}

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

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 ;(

user2127071
  • 23
  • 1
  • 3

3 Answers3

1

Serialization of classes is for example included in the boost package.

http://www.boost.org/doc/libs/1_53_0/libs/serialization/doc/index.html

I do not think you actually want to implement these kind of functionality yourself.

Coert Metz
  • 894
  • 6
  • 21
1

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) 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) 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.

Timo Geusch
  • 24,095
  • 5
  • 52
  • 70
0

You have to use pointer array of class PERSON. Then read from the binary file and populate array of persons.

ifstream input("PERSON.std",ios::binary);
    input.seekg(0, ios::end);
    int count = input.tellg() / sizeof(PERSON);
    PERSON *persons = new PERSON[count];
    input.seekg(0, ios::beg);
    input.read((char*) persons, sizeof(PERSON)*count);
    cout << count << endl;

    for (int j = 0; j < count; j++)
    {
        cout << count << endl;
        cout <<persons[j].output() << "\n";
    }
    cout << '\n';
    input.close();