I have a custom class called Array. It stores the array of type T and a size integer.
Does this look right saving an array to file?
fout.write((char *)m_array, sizeof(T) * m_size);
How I'm storing it:
bool save(const std::string &filename)
{
std::ofstream fout;
// Open the file.
fout.open(filename, std::ios::out | std::ios::binary);
// Validate that the file is open.
if (!fout.is_open())
return false;
// Write the size to file.
fout << m_size << std::endl;
fout.write((char *)m_array, sizeof(T) * m_size);
fout.close();
return true;
}
Load function:
bool load(const std::string &filename)
{
std::ifstream fin;
int size = 0;
// Open the file.
fin.open(filename, std::ios::in | std::ios::binary);
// Validate that the file is open.
if (!fin.is_open())
return false;
// Read the size from file.
fin >> size;
// Resize if needed
if (size != m_size)
resize(size);
fin.read((char *)m_array, sizeof(T) * m_size);
fin.close();
return true;
}
The main file:
Array<int> a(10);
a.clear(); // set all values to 0.
a[5] = 3;
if (a.save("test.dat"))
con.writeLine("Saved");
This gives the output values 0,0,0,0,0,0,3,0,0,0.
But on retrieval, using this:
fin.read((char *)m_array, sizeof(T) * m_size);
I'm getting 10,0,0,0,0,768,0,0,0,0.
What am I doing wrong? The second parameter says it wants the count, which would be sizeof(T) * m_size to me.
Update: The alternative is this on saving:
for (int i = 0; i < m_size; i++)
fout << m_array[i] << std::endl;
But I prefer the first method.
fin.get();afterfin >> size.