Search code examples
c++serializationfstream

Writing/reading raw objects to file


Problem: I need to write/read objects from a file.This because I need to write/read a std::list to file, but in whatever case.Not only with T=int (this would be simple), but with whatever parameter. In Java with OutputFileStream and InputFileStream this was possibile, but I suppose it's just a JVM feature. However I am trying to read/write objects to a file:

template <class T>
bool write_object(std::fstream& out, const T& object)
{
    bool result=false;
    char* ptr;
    const unsigned long size=sizeof(object);
    if(out.good())
    {
        result=true;
        ptr=(char*)&object;
        for(unsigned int i=0;i<size;i++)
        {
            out << *ptr;
            ptr++;
        }
    }
    return result;
}

template <class T>
bool read_object(std::fstream& in, T& object)
{
    bool result=false;
    T* ptr;
    T swap_temp;
    const unsigned long size=sizeof(object);
    char temp[size];
    std::streampos pos;
    if(in.good())
    {
        pos=in.tellg();
        if(pos!=-1)
        {
            result=true;
            for(unsigned long i=0; i<size; i++)
            {
                if(!in.good())
                {
                    result=false;
                    break;
                }
                else
                {
                    in >> temp[i];
                }
            }
        }
    }
    if(result==false)
    {
        in.seekg(pos);
    }
    else
    {
        ptr=(T*)temp;
        swap_temp=*ptr;
        object=swap_temp;
    }
    return result;
}

But I have encountered the following problems: -sizeof operator just returns the size of all fields, it does not consider also the data pointed by internal fields; -If in the class there is a pointer, then this pointer could point to a "wrong" memory address, (e.g.) if I use a pointer which points to a C-style string in memory, once the program ends the string is deallocated.When the instance of the program runs again,this area of memory could be anywhere.

This method is wrong because for example sizeof(string) with my compiler returns 4. So I suppose it uses a char pointer (I am on a 32-bit machine) to point to the C-style string allocated.Probably it does not even keep trace of the length. So if the string has 32 characters I don't notice it, it just copies the value of the pointer.


Solution

  • Your approach can't work since C++ doesn't know java-like techniques like reflection so you can't distinguish between pointers and other members.

    What you want is called serialisazion and you can use it with libraries like Boost.Serialization (Demo).

    But even then, you can't write a general function, you have to define it specifically for each object.