How to save a std::vector of C++ objects in binary format to disk?

c++ serialize object to file
how to create a binary file in c++
c++ write object to binary file
c++ read file into class object
c++ write to file
c++ read binary file into string
c++ read binary file into vector
ofstream

I have vector of base class pointers of potentially heterogeneous objects (all derived from the same base class). I would like to save this vector to disk in a binary format. Is there a library in C++ to store object and stl containers in binary format?

Serialization and Unserialization, C++ FAQ, It lets you take an object or group of objects, put them on a disk or send them Binary format can produce smaller results when most numbers are large or when you If you use C++-like escape-sequences for your string data, be sure to always When unserializing, use std::vector<std::string> unique , read the number n  I have vector of base class pointers of potentially heterogeneous objects (all derived from the same base class). I would like to save this vector to disk in a binary format. Is there a library in C++ to store object and stl containers in binary format?

I would suggest Google's protobuf library, which is designed just for solving this problem. It is binary, so not at all human readable. https://developers.google.com/protocol-buffers/

Writing contents of an std::vector to a binary file!, But I want to write the contents of that std::vector out to a binary file. Up to now it's used this command for a regular array: Code: [View]. There is some easy to understand information here: Input/output with files You should ideally aim for a flexible system where you can store many different types of data (you could look into xml read/write libraries or boost serialisation).

Consider this:

class Base {
protected:
    unsigned int id_;
protected:
    Base() {}
    virtual ~Base(){}
};

class DerivedA : public Base {
public:
    int someIntA_;
    short someShortA_;
};

class DerivedB : public Base {
public:
    int someIntB_;
    double someDoubleB_;
};

void writeToFile( const std::string& filename, std::vector<Base*>& objs ) {
    // first few elements in objs
    objs[0] = DerivedA;
    objs[1] = DerivedB;
    objs[2] = DerivedB;
    objs[3] = DerivedA;

    // how to pack & extract data in binary?
}

Your vector contains pointers to Base which depending on system and architect will determine the sizeof() a pointer type. Most systems have 4 bytes for a pointer type but this isn't guaranteed. However the data types at which these pointers point to are of their derived types. In memory sizeof(DerivedA) will be different than sizeof(DerivedB). You have to inform your C++ Compiler on how to parse this data in both writing to file and reading - extracting from file and this doesn't include any kind of compression techniques that may be used.

Now as for any auto generating libraries that will serialize your data for you; I'm not familiar of any off of the top of my head, but I have seen that others have given you some suggestions that may be worth looking into, otherwise you are on your own at writing to and from files according to your data types and their structures.

Binary Files in C++, Accessing a binary file from a C++ program (by not using the old C functions) The format that I've used indicates that the mode and prot arguments are optional​. The first argument is always the name of the file on the disk that the stream will be int main() { ifstream infile; infile.open("hello.dat", ios::binary | ios::in); // rest of​  Because most of the data that is held by a C# application at runtime is in the form of objects, it is convenient to be able to save and load objects to file directly. Such capability is dubbed object serialization, and like many other programming languages, C# has the facilities to perform object serialization for developers.

Read/Write Class Objects from/to File in C++, // objects in C++. #include <iostream>. #include <fstream>. using namespace std;​. An introduction to saving Unity classes as binary files. This is a great solution for saving player game progress on local builds, as well as storing external content that you still want to

C++ : How to read or write objects in file, Binary Search Tree · Binary Tree · Linked List In this article we will discuss how to write objects to file and how to read them back from std::string mPhoneNumber; #include <vector> C++ & C++11 Recommendations:  Smaller binary files open and save much faster than Excel workbooks (.xlsx or .xlsm) Binary file compression offers potential cost savings, because it reduces the disk space required to store files and decreases the bandwidth needed to transport files through e-mail or networks.

FlatBuffers: Use in C++, The code to test the C++ library can be found at flatbuffers/tests . you would read a FlatBuffer binary file in C++: First, include the library and generated code. The following attributes are specific to the object-based API code generation: The allocator is also used by any std::vector that appears in a table defined with  Must be a file name for save.image or version = 1. ascii: if TRUE, an ASCII representation of the data is written. The default value of ascii is FALSE which leads to a binary file being written. If NA and version >= 2, a different ASCII representation is used which writes double/complex numbers as binary fractions. version: the workspace format

Comments
  • The short answer is: no there isn't. You have to write all this code, all by yourself.
  • The task depends on 1. which C++ compiler implementation you are using, 2. which version of C++ runtime the code is compiled with.