我有一个名为如何将对象的模板化阵列保存到文件?
Array<T>
您可以创建任何类型的数组类。
template <typename T>
class Array
{
private:
T *m_array;
int m_size;
...
例如,
Array <int> myArray(32)
是int类型的数组的大小为32。这可以存储基本类型或复杂的对象。例如,
Array<Monster> monsters(32)
可以容纳一个怪物对象的数组。无论使用什么类型,我想从磁盘保存和加载数组。
其中一个对象,比如Actor,有一个类型为std :: string的成员变量(名称)。因此,它被存储为
Array<Actor> actors(32)
我今天意识到,C'S I/O功能一无所知的std :: string,所以装载的std ::从文件的字符串是在关机导致飞机坠毁。我想将我的Save/Load函数升级到C++的等价函数。我的假设是这将解决我的问题,保存和加载具有std :: string类型的成员变量的对象。我的原始保存/加载函数: (因为它们由于模板的工作原因而在头文件中,我应该提到它们是Array的更正式成员,或者Array :: save()和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;
}
负载:
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;
}
总体来说,我想这些转换成C++的文件处理。
这是我的C++的尝试与保存():
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;
}
所以,我的问题是我如何保存到文件时,它的模板类型可以是基本数据类型,结构或类阵列。我的第一个假设是:
// Write the array to file. ????
fOut.write(m_array, m_size);
任何想法都会有帮助。谢谢。
发现我需要序列化,我为我的Actor类重载了运算符< <,但希望进一步指导如何将其用于此目的。 Actor有一个需要保存到文件的std :: string。
std::ofstream & X2D::operator<<(std::ofstream & output, const Actor & p)
{
// insert magical code here
return output;
}
你在找什么叫做序列化。 –
_“由于模板的工作原因,它们位于标题中”_作为旁白,您应该考虑将这些定义从类定义中移出以获得更清晰和更好的代码。他们仍然可以在头上。 –
_“我有一个名为[..]的类你可以创建任何类型的数组。”_std :: array有什么问题? –