Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

How would I write/read data to a file in binary, if I also have to define how to save my data?
I'm attempting to save some simple data structures out to a file in binary.

For example, I have a vector of structs like this:

struct Vertex
{
    x;
    y;
    z;
}

std::vector<Vertex> vertices;

I want to save this vector out to a file in binary.

I know how to output it using ifstream and ostream using the << and >> operators, which can be overloaded to handle my data, but they can't output binary.

I also know how to use .write() to write in binary, but the issue there is that I can't find a way to overload what I need, in order to handle my data.

share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

Here is an answer to a similar question. While this is ok in your case, be aware that if you are using pointers in your struct, this will not work.

The pointer means : "There is relevant data loaded in this other memory segment", but really, it only contains the address of the memory. Then the write operation will save this memory location. When you will load it back, there is very little chance that the memory still holds the information you want.

What people usually do is create a serialization mechanism. Add a method to your struct, or write another function that takes your struct as a parameter and outputs a char* array containing the data in a special format you decide. You will also need the opposite function to read from a file and recreate a struct from binary data. You should take a look at boost::serialization which handles this very common programming problem.

share|improve this answer
 
Will this method work with std::vector ? –  user947871 May 2 '13 at 15:38
 
maybe, probably not. You should not attempt to serialize classes that are not explicitely made for it. Instead you should create a data format for your save. Like the first int in the file will be the number of records followed by the actual records –  Eric May 2 '13 at 15:46
 
I am going to use BOOST::Serialization since I can see it being useful in the future. I have stayed away from it before due to the Boost documentation for it being very cluttered. If anyone else sees this, I recommend looking here for an easier to follow guide : ocoudert.com/blog/2011/07/09/… –  user947871 May 2 '13 at 15:53
add comment

One way of doing it (not necessarily the best) is to write the data, using whatever binary write function you choose, eg

write(fd,data,size);

But pass the "data" as the struct.

eg

Vertex data;
data.x = 0;
etc...
write(fd,(char*)&data,sizeof(data));

which will treat the struct as an array of characters, and then write them to the file. Reading it back in should be the reverse of the above.

Note that there is not really a nice way to do this with vectors (which are dynamically allocated and my have strange things in strange places in memory), so I recommend an array of structs.

share|improve this answer
 
so, are you saying that instead of trying to output a vector in one call, I should iterate through it, and write to the file multiple times? –  user947871 May 2 '13 at 15:39
 
Yes. This is what I would recommend. –  rspencer May 4 '13 at 19:05
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.