Writing object into binary file

Jan 27, 2016 at 7:09pm
I have an object declared as such:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

class NodeType
{
private:
	int value;
	int doctorID;
	string first;
	string last;
	NodeType *next;
	friend class Patient;
};

class Patient
{
private:
	NodeType *head;
public:
	Patient();
	bool empty();
	void addValue(int, int, string, string);
	void addFront(int, int, string, string);
	void removeValue(int);
	void print();
	int getSize();
	int getVal(NodeType *&temp);
	int getDoctorID(Patient *&temp);
	string getFirst(Patient *&temp);
	string getLast(Patient *&temp);
	void writeFile();
	~Patient();
};


As you can see, this object contains 4 values I would like to be written onto a binary file


The normal method I use to write to a binary file is this:

1
2
3
4
5
6
7
void File::writeBinary(string name)
{
	ofstream b(name, ios::out | ios::binary);
	b.write((char*)&x, sizeof(x));
	b.write((char*)&y, sizeof(y));
	b.close();
}


However, does this method work with writing a whole object?


Is there a way I could just write this whole object Patient, for example Patient One(); onto a binary file?
Jan 27, 2016 at 7:14pm
When you say "binary file", do you mean an executable file or do you just want the stream operating in binary mode?
Jan 27, 2016 at 7:45pm
However, does this method work with writing a whole object?

No, because you have some non-trivial classes in your class you can't write the whole object at once.
Jan 27, 2016 at 8:17pm
A binary file as in a file that has the extension ".bin"


What does non-trivial classes mean?

So then how would you write an object to a binary file?
Jan 27, 2016 at 8:44pm
So then how would you write an object to a binary file?
See for example Google's protobuf, or Apache Avro.
Unfortunately, there are no simple, general solutions.
Jan 27, 2016 at 8:46pm
Haha what!! I'm in an intermediate c++ class in uni and one of my assignments is to write a similar object into a binary file and we didnt learn google protobuf or apache avro, is there really simple solution to this problem?
Jan 27, 2016 at 9:02pm
What does non-trivial classes mean?

Usually this means classes that have pointer data members. In your case you nave a NodeType* and a couple of std::strings.

So then how would you write an object to a binary file?

You'll need to write out each individual member. For members like std::string you'll probably need to first write the size of the string then write the string using the c_str() member function. You'll have to decide what you want to do with that NodeType pointer.

Jan 27, 2016 at 9:23pm
Ah I see, so I should create "get" functions to retrieve the values and then write them onto the binary file as individual values correct?
Jan 27, 2016 at 11:14pm
If you're going the object route, it would be better to add a function to the class such that it writes itself to file. Nice and neat and self-contained.
Jan 28, 2016 at 9:29pm
Yeah I understand that

So if i create a member function to write the object into the file, how do I do that??

Can I just write directly the attributes of the object into the binary file? Or do I write the whole OBJECT into the file?
Jan 29, 2016 at 4:09pm
Either approach could be made to work but writing only the attributes into a standardized format in discrete values is usually neater. It also makes for easier debugging if the raw data is in a format that you can read.
Topic archived. No new replies allowed.