Best Way To Deal With Binary Files?

Started by
12 comments, last by Low Bias 19 years, 7 months ago
I want to starting reading binary 3D model files (gamespace .cob files specifically). What's the most correct / current C++ technique to use these days? If I'm going to refresh this part of my coding arsenal, I might as well use what is currently deemed best practice, no? Any advice or ideas on reading binary files are welcome!
Advertisement
fstream. It's a little ugly to use, casting stuff to char * and all with ifstream::read, but I like it :)
Quote:Original post by Low Bias
I want to starting reading binary 3D model files (gamespace .cob files specifically). What's the most correct / current C++ technique to use these days? If I'm going to refresh this part of my coding arsenal, I might as well use what is currently deemed best practice, no?

Any advice or ideas on reading binary files are welcome!

The simplest (once it's setup) way of reading from binary files is probably to use memory mapped files.
Do you have a link to something I could read that discusses the idea of memory mapped files?

Ideally, I would like to read entire chunks of the file right into structs. I don't know if there's a nice way to do so, but it would be ideal.
Quote:Original post by Low Bias
Do you have a link to something I could read that discussed memory mapped files?

Ideally, I would like to read entire chunks into memory right into structs. I don't know if there's a nice way to do so, but it would be ideal.

That's what memory mapped files are.
Tutorial

The nice thing about it is that data is only read on demand when you touch it, you don't have to precache the whole file.
Those memory mapped files sound great! Any disadvantage to using them? Looks like exactly what I was looking for.

Thanks for your help. :)
Quote:Original post by Low BiasAny disadvantage to using them?
Portability, no async io..
Nothing major really.
A related question for you...

If I'm reading this entire struct in from the file...

struct cob_header
{
char cob_id[9];
char cob_version[6];
char cob_format;
char cob_endian[2];
char cob_ignore[13];
};

What is the best way to terminate the strings / character arrays? Should make each array one element bigger (to accomodate the terminating 0), zero the memory first, then read in each member individually?
Hey, Doynax! I was just in Sweden this past weekend! :)
Quote:Original post by Low Bias
A related question for you...

If I'm reading this entire struct in from the file...

struct cob_header
{
char cob_id[9];
char cob_version[6];
char cob_format;
char cob_endian[2];
char cob_ignore[13];
};

What is the best way to terminate the strings / character arrays? Should make each array one element bigger (to accomodate the terminating 0), zero the memory first, then read in each member individually?
How about not treating them as null terminated strings at all for as long as possible?
I doubt you'll need to convert many of those fields for example. This is what C's string library was designed for in the first place (that's why strncpy doesn't terminate properly).

edit: In the case of memory mapped files you won't need to read anything at all. Just cast the mapped block to a cob_header and that's it.

This topic is closed to new replies.

Advertisement