It is possible to convert the following Student class to a binary file by writing it with ios::binary
#include <fstream>
#include <iostream>
using namespace std;
class Student{
public:
char name[40], address[120], gender;
double age;
bool is_blabla;
};
int main() {
Student one;
strcpy(one.name, "Cancan Can");
strcpy(one.address, "example example exampla");
one.gender = 'M';
one.age = 25;
one.is_blabla = true;
ofstream ofs("fifthgrade.ros", ios::binary);
ofs.write((char *)&one, sizeof(one));
Student two;
ifstream ifs("fifthgrade.ros", ios::binary);
ifs.read((char *)&two, sizeof(two));
// check if the data is OK
cout << "Student Name: " << two.name << endl;
return 0;
}
The file looks like as follows:
http://i.imgur.com/GfkheWs.png
So how can I do the same thing to convert the class to a byte (or say char*) array in this case?
Edit: But in this case, Let's say I have a class having methods. The answers you gave is telling me to write my own bitwise operators to serialize a complex class. Can you refer to a good source teaching how it can be done or a small sample/example for it?
Edit2: I have to avoid using extra libraries because I may deserialize the serialized code on a machine/compiler that I cannot import most of the libraries (for example I will try to deserialize the code on an nvcc compiled code).
Example dummy class would be like this:
class Student{
public:
char name[40], address[120], gender;
double age;
bool is_blabla;
void set_values (int,int);
int doubleage() {return age*2;}
};