Hi there,
I'm trying to teach myself some things about random file I/O with binary, and have come across a task that I am unsure I know how to approach.
Currently I write out a bunch of variable length structs to a binary file. They look something like this
When writing out I am writing the length of the string before I write the string, as an unsigned long, so that I can know how long the string will be. After writing out 10 of theses to disk, I then read all 10 back in as a single chunk of binary data, using ifstream.read() , and store it in a char array.
What I want to do is then go through this array and, for want of a better term, extract the actual types and data out of the array. I have tried casting, but that just truncates, and I have tried bit shifts, but I think that I don't understand those well enough to actually make it work.
Can someone suggest a way to do this? Is bit shifting a good approach? Is there something that I haven't mentioned that could be used? It would be much appreciated.
Thanks,
Dylan Jenkinson
I'm trying to teach myself some things about random file I/O with binary, and have come across a task that I am unsure I know how to approach.
Currently I write out a bunch of variable length structs to a binary file. They look something like this
Code:
struct testStruct
{
std::string term;
unsigned long termCount;
};
What I want to do is then go through this array and, for want of a better term, extract the actual types and data out of the array. I have tried casting, but that just truncates, and I have tried bit shifts, but I think that I don't understand those well enough to actually make it work.
Can someone suggest a way to do this? Is bit shifting a good approach? Is there something that I haven't mentioned that could be used? It would be much appreciated.
Thanks,
Dylan Jenkinson
Comment