Jump to content
  • Advertisement
Sign in to follow this  
riyunoa

Loading binary files?

This topic is 4832 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
Advertisement
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;
inFile.open("foo", std::ios::binary);

// now let's read the first byte
char byte;
inFile.read(&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];
inFile.read((char*)array, sizeof(int));

// ...and close the file
inFile.close();

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!