1

So I make a serialization of a single object but I had problem with several. Here is the code:

    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    class MyTest
    {
    private:
        string test;
    public:
        MyTest():test(""){};
        void setTest(const string& test) {this->test = test;};
        string getTest() const {return this->test;};
    };
    void writeToFile(const MyTest& m)
    {
        ofstream ofs("data.mbp", ios::app|ios::binary);
        ofs.clear();
        ofs.write((char *)&m, sizeof(m));
        ofs.flush();
        ofs.close();
        return;
    };
    MyTest& readTest(MyTest& m,int num)
    {
        ifstream ifs;
        ifs.open("data.mbp", ios::in|ios::binary);
        for ( int i = 1 ; i <= num ; i++)
            ifs.read((char *)&m, sizeof(m));
        return m;
    }

    int main(int argc,char* argv[])
    {
        MyTest m,t;
        m.setTest("Hello");
        writeToFile(m);
        t.setTest("World");
        writeToFile(t);
        t = readTest(t,1);
        cout << t.getTest() << endl;

        m = readTest(m,2);
        cout << m.getTest() << endl;


        return 0;
    }

The problem is that I do not know how to write two or more objects in a binary file and after that how can I read them. Does anybody know?

Thanks in advance.

Jordan Borisov
  • 1,603
  • 6
  • 34
  • 69
  • You are doing it wrong in a first place — `std::string` is using dynamically allocated memory, and you are not handling that even when writing... :( –  Jul 29 '12 at 19:29
  • I have to admit that I'm a java developer :) – Jordan Borisov Jul 29 '12 at 19:30
  • You forgot to serialize the data! For example, your `writetofile` opens a file, writes data that is *not* serialized to the file, and the flushes and closes it. The data you've written is now nonsense. (If you think it's sensible, then answer me this: What does the first byte contain? What does the last byte contain? You have *no* idea because you never serialized it!) – David Schwartz Jul 29 '12 at 19:35
  • in java to serialize a object the class wich this object is instance of have to implement Serializable interface and in the file there is a header which the ObjectOutputStream read and deserialed it. But I do not know how to do it right in c++? – Jordan Borisov Jul 29 '12 at 19:38
  • You can do it that same way if you like. – David Schwartz Jul 29 '12 at 23:53

2 Answers2

5

I recommend you using Boost - Serialization for serializing objects in C++: http://www.boost.org/libs/serialization/

ildjarn
  • 62,044
  • 9
  • 127
  • 211
Stefan Profanter
  • 6,458
  • 6
  • 41
  • 73
  • ok but I even do not how to use this library. I downloaded it yesterday and compile it but I do not know how to use it and put it in VS 2010 – Jordan Borisov Jul 29 '12 at 19:33
  • 1
    See [Getting Started on Windows](http://www.boost.org/doc/libs/1_35_0/more/getting_started/windows.html) or [Use Boost Serialization Library in VC++ 2010 Project](http://stackoverflow.com/questions/4879665/use-boost-serialization-library-in-vc-2010-project) – Stefan Profanter Jul 29 '12 at 19:37
  • a little bit problem with boost: 1>LINK : fatal error LNK1104: cannot open file 'libboost_serialization-vc100-mt-gd-1_50.lib' – Jordan Borisov Jul 29 '12 at 19:59
  • Regarding the LNK1104 error: you have to add the library path to you project: http://stackoverflow.com/questions/5552788/c-link-error-cannot-open-file-omniorb414-rtd-lib – Stefan Profanter Jul 30 '12 at 07:05
2

There are a lot of different ways of doing that. You need to select a file format first. Think about XML in the first hand. Serialization of complex data structures is better to base on some existing library rather than write it yourself from scratch. Search Inet for such libraries.

Kirill Kobelev
  • 10,252
  • 6
  • 30
  • 51