私はあなたが任意の型の配列を作成することができますオブジェクトのテンプレート配列をファイルに保存するにはどうすればよいですか?
Array<T>
というクラスを持っています。例えば
template <typename T>
class Array
{
private:
T *m_array;
int m_size;
...
、
Array <int> myArray(32)
は、基本型または複合オブジェクトを格納することができる32本の大きさのint型の配列です。例えば、
Array<Monster> monsters(32)
は、モンスターオブジェクトの配列を保持することができます。どんなタイプが使われても、ディスクからアレイを保存してロードしたいと思います。
Actorのようなオブジェクトの1つに、タイプがstd :: stringのメンバ変数(name)があります。だから、私がCのI/O機能は、STDについて何も知らないこと::文字列を、そのファイルからのstd :: stringはシャットダウン時にクラッシュを引き起こしていたロード今日実現
Array<Actor> actors(32)
として格納されています。私は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'の何が問題なのですか? –