Advertisement Jump to content
Sign in to follow this  

Loading binary files?

This topic is 4903 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

Okay, recently I've been looking at the Quake 3 BSP format... It seems to be a binary file format and not human-readable. So my question is, how do I write a loader that loads these kind of binary files, when I can't even read the file data with my own eyes? I'm quite confused because I have a whole list of specs, but how on earth do I use that info to load the file, when the file isn't human text?

Share this post

Link to post
Share on other sites
The file specifications should tell you what each and every byte in the file is for. What do you have a problem with, precisely? Sure, numbers are not written in ASCII-coded decimal format, but they're still there. Assume the size of an integer is 4 bytes (i.e. sizeof(int) == 4), then when you save/load it from a binary file, it's those 4 bytes you will write/read, directly.

Share this post

Link to post
Share on other sites
Passing "rb" to the fopen() call will open a file for reading as binary, then just call fgetc() to get each byte. (Assuming you're using C).
Wotsit's Format is a great resource for a host of different file types.
EDIT: I was assuming directly reading in bytes. For some formats it makes more sense to read in ints or shorts as well. I don't know how the BSP format is arranged, so cannot comment.

Share this post

Link to post
Share on other sites
You should look into the fstream object.
It could look something like this

#include <fstream>

int main()
// assume you have a binary file "foo" that you want to open in binary mode
std::ifstream inFile;"foo", std::ios::binary);

// now let's read the first byte
char byte;, sizeof(char));

// ok, let's say our next info is 100 bytes into the file(from the beginning of the file)...
inFile.seekg(100, std::ios::beg);

// and an array of int's
int array[10];*)array, sizeof(int));

// ...and close the file

return 0;

I can't guarrantee that this code is absolutely correct, i just typed it down from the top of my head. It should however, in it's simplest form, be about what you need to know to get the basic understanding of reading binary files =)

Share this post

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

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. 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!