Loading binary files?

Started by
2 comments, last by Android_s 18 years, 8 months ago
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?
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.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
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.

[Website] [+++ Divide By Cucumber Error. Please Reinstall Universe And Reboot +++]

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 =)
----------------------------------------------------------------------------------------------------------------------"Ask not what humanity can do for you, ask what you can do for humanity." - By: Richard D. Colbert Jr.

This topic is closed to new replies.

Advertisement