You can save the entire structure as binary, heres an exmaple of how to:
#include <iostream>
#include <fstream>
using namespace std;
template<typename type>
void SaveStruct(ofstream &out, type &obj) {
out.write(reinterpret_cast<char*>( &obj ), sizeof type);
}
template<typename type>
void LoadStruct(ifstream &in, type &targetObj) {
in.read(reinterpret_cast<char*>( &targetObj ), sizeof type);
}
struct A {
char str[20];
size_t str_len;
};
int main() {
// Make Structure
A a;
strcpy_s(a.str, 20, "Hello World");
a.str_len = strlen(a.str);
// Save structure to file using SaveStruct(...)
ofstream out("savefile.txt", ios::out | ios::binary);
SaveStruct(out, a);
out.close();
// Load structure from file into the variable b
A b;
ifstream in("savefile.txt", ios::in | ios::binary);
LoadStruct(in, b);
// Display struct b
cout << "str: " << b.str << "\nstr_len: " << b.str_len;
cin.ignore();
return 0;
}