I have a class called
Array<T>
You can create an array of any type.
template <typename T>
    class Array
    {
    private:
        T *m_array;
        int m_size;
        ...
For example,
Array <int> myArray(32) 
is an array of type int with a size of 32. This can store basic types or complex objects. For instance,
Array<Monster> monsters(32)
can hold an array of monster objects. Whatever type is used, I would like to save and load the array from disk.
One of these objects, say Actor, has a member variable (name) of type std::string. So, it is stored as
Array<Actor> actors(32)
I realized today that C's I/O functions know nothing about std::string, so loading std::string from file was causing a crash on shutdown. I want to upgrade my Save/Load functions to C++'s equivalent. My assumption is this will solve my problem with saving and loading objects that have a member variable of type std::string.
My original Save/Load functions: (Because they're in a header due to how templates work, I should mention they are more formally members of Array, or Array::save() and Array::load().)
            bool save(const string filename)
        { 
            FILE *fOut = NULL; 
            int written = 0;
            // Validate the array
            if (!isValidArray())
                return false;
            // Open the file
            fOut = fopen(filename.c_str(), "wb");
            if (fOut == NULL)
                return false; 
            // Write the array's size to file.
            fwrite(&m_size, sizeof(int), 1, fOut);
            // Write the array to file.
            written = fwrite(m_array, sizeof(T), m_size, fOut);
            fclose(fOut);
            // Validate if the array was written correctly
            if (written != m_size)
                return false; 
            return true;
        }
Load:
bool load(const string filename)
        {
            FILE *fIn = NULL;
            int read = 0;
            int size = 0;  
            // Open the file
            fopen_s(&fIn, filename.c_str(), "rb");
            if (fIn == NULL)
                return false;
            // Read the array's size from file.
            fread(&size, sizeof(int), 1, fIn);
            // Rleease the old array 
            release();
            // Initialize the new array
            if (!init(size))
                return false;
            // Read the array from file.
            read = fread(m_array, sizeof(T), size, fIn);
            fclose(fIn);
            // Validate if the array was written correctly.
            // If not, clean up the array object.
            if (read != size)
            {
                if (m_array != NULL)
                {
                    delete[] m_array;
                    m_array = NULL;
                    m_size = 0;
                }
                return false;
            } 
            return true;
        }
Overall, I would like to convert these to C++'s file handling.
This is my C++ attempt with save():
        bool save(const string filename)
        {  
            ofstream fOut; 
            // Validate the array
            if (!isValidArray())
                return false;
            // Open the file
            fOut.open(filename.c_str(), std::ios::binary | std::ios::out);
            if (!fOut.is_open())
                return false; 
            // Write the array's size to file.
            fOut << m_size;  
            // Write the array to file. ???? 
            fOut.write(m_array, m_size);
            fOut.close();
            return true;
        }
So, my problem is how do I save the array to file when its templated type could be a basic data type, struct, or class. My first assumption was this:
// Write the array to file. ???? 
fOut.write(m_array, m_size);
Any thoughts would be helpful. Thank you.
Finding out I need serialization, I overloaded operator<< for my Actor class, but would like further guidance on how to use it for this purpose. Actor has a std::string that I need to save to file.
std::ofstream & X2D::operator<<(std::ofstream & output, const Actor & p)
{ 
    // insert magical code here 
    return output;  
}
 
    