I was looking for an efficient and compact way to store data permanently but wasn't able to find any executable minimal example so I put it together for you.
The beauty of this solution is the ability to handle the data in the 'vector' as it pleases and to expand the 'struct' as it pleases (with minor changes)
That way the 'vector' as represented in the memory is transferred to the drive and back again with the 'data()' method 'std::vector' is providing.
#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;
}
}