我一直在寻找一种有效且紧凑的方式来永久存储数据,但找不到任何可执行的最小示例,所以我为您整理了它。
这个解决方案的美妙之处在于能够随心所欲地处理“向量”中的数据并随心所欲地扩展“结构”(稍作改动)
这样,内存中表示的“向量”将通过“std::vector”提供的“data()”方法传输到驱动器并再次返回。
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#define FILENAME "data.bin"
struct Vertex
{
size_t x{};
size_t y{};
std::string z{};
};
void writeVectorToBinary(std::vector<Vertex> &v);
void readVectorFromBinary(std::vector<Vertex> &v);
void printVector(std::vector<Vertex> &v);
int main()
{
std::vector<Vertex> vertices;
vertices.push_back({1,2,"three"});
vertices.push_back({4,5,"six"});
writeVectorToBinary(vertices);
printVector(vertices);
readVectorFromBinary(vertices);
printVector(vertices);
std::cin.get();
return 0;
}
void writeVectorToBinary(std::vector<Vertex> &v)
{
size_t size = v.size();
//Open Binary file, to write out data
std::ofstream file(FILENAME, std::ios::binary);
if(!file)
std::cout << "Something went wrong" << std::endl;
//Store/Write the vector size
file.write(reinterpret_cast<const char*>(&size), sizeof(size));
//Store/Write the data of the vector out
file.write(reinterpret_cast<const char*>(v.data()), sizeof(v[0])*size);
//Close binary file
file.close();
}
void readVectorFromBinary(std::vector<Vertex> &v)
{
//Clear Vector just for the proof of concept
v.clear();
size_t size{};
//Open Binary file to read in data
std::ifstream file(FILENAME,std::ios::binary);
if(!file)
std::cout << "Something went wrong" << std::endl;
//Read the vector size
file.read(reinterpret_cast<char*>(&size), sizeof(size));
//Resize vector now that its known
v.resize(size);
//Read vector data in
file.read(reinterpret_cast<char*>(v.data()), sizeof(v[0])*size);
//Close binary file
file.close();
}
void printVector(std::vector<Vertex> &v)
{
for(size_t i{}; i < v.size(); ++i ){
std::cout << "\n------Vector" << i << "--------" << std::endl;
std::cout << v[i].x << std::endl;
std::cout << v[i].y << std::endl;
std::cout << v[i].z << std::endl;
}
}