2

For example the following class:

class example
{
    public:
        int *numbers;//int pointer
        int size;

        example(int s)
        {
            size=s;
            numbers=new int [size];
        }

        ~example()
        {
            delete []numbers;
        }
}

How can this class be stored in a file ?

nbro
  • 15,395
  • 32
  • 113
  • 196
zeeshan090
  • 81
  • 5

4 Answers4

1

There is no easy solution to this question - simply put pointers represent a location in memory but when you restore data from the file you expect it to be stored somewhere else (i.e. not in the original location) so storing the value of the pointer in anyway will fail.

Instead you need to set the pointers as you load from the file to relink with the correct memory location. Generally the tactic used to handle this sort of problem is to store a numeric ID for each record - as you load the ID from the file that you need to link to you look up the ID from what is already loaded and set the pointer to that record. This process is surprisingly called pointer swizzling (thanks @keyser).

This problem is generally referred to a serialization of a class this question and this question are good places to start reading about this process on Stack Overflow.

There are widely used tools for this kind of problem; take a look at the boost::serialzation documentation.

Community
  • 1
  • 1
Elemental
  • 7,365
  • 2
  • 28
  • 33
1

Untested, but gives you an idea.

class Example
{
public:
    vector<int> numbers;

    explicit Example(int s = 0)
        : numbers(s)
    {
    }

    friend ostream& operator << (ostream& os, const Example& x)
    {
        os << x.numbers.size() << " ";
        for (auto n : x.numbers)
        {
            os << n << " ";
        }
        if (os.bad()) ERROR;
        return os;
    }

    friend istream& operator >>  (istream& is, Example& x)
    {
        vector<int>::size_type size;
        is >> size;
        numbers.resize(size);
        for (auto& n : x.numbers)
        {
            is >> n;
        }
        if (is.bad()) ERROR;
        return is;
    }
};

{
    ofstream outfile("myfile.txt");
    Example x;
    outfile << x;
}
{
    ifstream infile("mytext.txt");
    Example x;
    infile >> x;
}
Neil Kirk
  • 21,327
  • 9
  • 53
  • 91
0

This is a very broad topic and there are many ways of solving the problem. One popular way of doing class serialization now is using Google Protocol Buffers.

snowcrash09
  • 4,694
  • 27
  • 45
0

You need to implement a serialization method. Any class that needs to be serialized should know how to selialize and deserialize itself. You can choose one of the common human readable formats available formats, such as JSON, XML etc. You also have the option of using more compact formats such as Google protocol buffers, Apache thrift, or msgpack. If your serilization needs are very simple, then another option is to roll out your own. Here is a full compilable version of the answer given by Neil Krik which should help your understanding . . .

#include <iostream>
#include <vector>
#include <sstream>
#include <assert.h>
#include <fstream>

using namespace std;

class Example
{
    vector<int> nums;

public:
    explicit Example(vector<int> s = {}) : nums(s)
    {
    }

    vector<int>& getNums()
    {
        return nums;
    }

    vector<int> getNums() const
    {
        return nums;
    }

    string stringify() const
    {
        stringstream s;
        for (auto x : nums) {
            s << x << ", ";
        }

        return s.str();
    }

    friend ostream& operator << (ostream& os, const Example& x)
    {
        os << x.getNums().size() << " ";
        for (auto n : x.getNums())
        {
            os << n << " ";
        }
        assert(os.good());
        return os;
    }

    friend istream& operator >>  (istream& is, Example& x)
    {
        vector<int>::size_type size;
        is >> size;
        x.getNums().resize(size);
        for (auto& n : x.getNums())
        {
            is >> n;
        }
        assert(is.good());
        return is;
    }
};


int main()
{
    // Example 1
    string store;
    {
        ostringstream outfile;
        Example x( {1,2,3,4,5,4,3,2,1} );
        outfile << x;

        store = outfile.str();
    }
    {
        istringstream infile(store);
        Example x;
        infile >> x;

        std::cout << x.stringify() << std::endl;
    }

    // Example 2
    {
        ofstream outfile("myfile.txt");
        Example x( {1,2,3,4,1,2,3,4} );
        outfile << x;
    }
    {
        ifstream infile("myfile.txt");
        Example x;
        infile >> x;

        std::cout << x.stringify() << std::endl;
    }

   return 0;
}
learnvst
  • 15,455
  • 16
  • 74
  • 121