Jump to content
  • Advertisement
Sign in to follow this  
Low Bias

Best Way To Deal With Binary Files?

This topic is 5446 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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!

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Those memory mapped files sound great! Any disadvantage to using them? Looks like exactly what I was looking for.

Thanks for your help. :)

Share this post


Link to post
Share on other sites
Quote:
Original post by Low BiasAny disadvantage to using them?
Portability, no async io..
Nothing major really.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!