Need Help with Reading/Writing 2Vector of Structs

I'm not new to c++ programming but I am new to reading and writing information to and from a binary file in c++. I've grasped some of the basics of reading and writing from online examples but I'm still having trouble. My question is, is it possible to read and write the vector 'directories' below to and from a binary file?

Any articles related to this would be great, along with example code or explanation on how the vector is read back and how to access that information to be read out.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>;
#include <fstream>;
#include <cstring>;
#include <string>;
#include <vector>;

struct File{
   string name;
   string data;
}

struct Directory{
   string name;
   vector<File> files;  
}

vector<Directory> directories;
My question is, is it possible to read and write the vector 'directories' below to and from a binary file?

Possible, yes, but also not so simple. You'll need to somehow write each element of your vector to the file, serializing each Directory and File as you go. Is there a reason you want to use a binary file instead of just a plain text file?

I'd like to use a binary file because it'll be faster to access and read the data, not to mention the size would be much smaller if I had a large amount of data input. But before you were saying that I would need to serialize each Directory and File, how so?


Something on the lines of doing this?

file.write((char*)&directories[i][0], directories.size() * sizeof(Directory))

file.write((char*)&files[0], files.size() * sizeof(File))


And vise versa with the read?
But before you were saying that I would need to serialize each Directory and File, how so?


Well since your classes contain non-trival classes (like std::string and std::vector) you can't write your classes without a little help. There are a couple of problems that need to be over come. First you don't know the true size of your data structures because they contain "hidden" pointers contained in those non-trival classes. Second you can't assume that you know the sizes for the classes because the lengths of the strings, and the number of elements in your vectors are not constant.

I'd like to use a binary file because it'll be faster to access and read the data,

This is only true if you need to do random access of your file, but remember that file access of a file is orders of magnitude slower than accessing memory. Quite often it is faster to read the whole file into memory and access the contents in memory.

not to mention the size would be much smaller if I had a large amount of data input.

This also not usually true. Remember that to get random access you must know the number of bytes you wrote into your file, which usually means writing fixed size strings, and fixed size "arrays" which can take up more memory than you expect.

Another problem with binary files is that it is harder to "see" the data and therefore harder to see "bad data". And lastly binary files are not portable and are much harder to "add" or "modify" the type of data that is being held in the file.

I really suggest you consider sticking with plain text files unless your file sizes are extremely large, in which case you should really be considering a true data base system.


Last edited on
Topic archived. No new replies allowed.