我想保存这个结构并在以后加载它:
struct Block
{
vector<int> numbers;
map<int , char> bits;
map<string , string > states;
string off;
}relationTable[7][11][13];
Run Code Online (Sandbox Code Playgroud)
除了使用几个for
循环之外还有什么方法吗?我使用此功能进行保存:
void saveData()
{
string location = "test.bin";
ofstream fs(location, std::ios::out | std::ios::binary | std::ios::app);
fs.write(reinterpret_cast<const char *>(relationTable), sizeof(relationTable));
fs.close();
}
Run Code Online (Sandbox Code Playgroud)
这一个负载:
void loadData()
{
string location = "test.bin";
Block array[64][96][2];
ifstream file (location, ios::in|ios::binary|ios::ate);
if (file.is_open())
{
file.seekg (0, ios::beg);
file.read ((char*)&array, sizeof(array));
file.close();
}
...//some use of array
}
Run Code Online (Sandbox Code Playgroud)
但它不起作用,因为它只保存指针!我该怎么办?