Page 1 of 1

[SOLVED] Help! Reading in binary file C++

Posted: Thu Mar 11, 2010 11:35 pm
by LeonBlade
Hey guys,

If you've seen my project I've been working on, I want to work on maps.
I've created a map file which documentation can be found here.

My problem is, I know what I want to do, I just don't know how I need to do it.
What I need to do is open the map file, and then start reading in bytes.

First off I need to read in the "magic" value of BBMF for Blade Brothers Map File (the map file type for my engine) which is 4 bytes.
After that is 4 bytes which contains the file size.
Then comes the details section of the file which currently just holds 2 bytes for the tiles wide and tiles high both 2 bytes each like I said so a total of 4 bytes for those two.
Then finally is the map data which is 3 bytes per tile. Tiles get loaded in from top left to bottom right. Each byte is from 0-FF which is 0 to 255 and that represents which tile number to grab on a corresponding tilesheet (to be worked out later).
The bytes are laid out in a left to right/top to bottom approach like you would read standard English text.

NOW, here is my issue.
I need to open the file so that I may read in the bytes.
First I need to start at 0 and read the first 4 bytes of data, the I need to move my read pointer to 4 bytes in and read 4 more bytes for the filesize, then move from there 4 bytes again over and then read in 2 bytes for the tiles wide move 2 bytes read in 2 bytes again for the tiles high move 2 bytes and then start reading in each tile which is reading in 3 bytes then moving 3 bytes from there to the next tile and so and and so forth.

That was a run on sentence, however, I don't care! ;)
My problem is, I don't know how to do this in C++ :roll:

I've tried a few approaches and everything seems to be leading me in the wrong direction or something.
Can someone assist me? I just need to know a few function calls for what I want to do and I can be on my way :)
Also, inb4 someone says "you should start off with something more easier" ;) I know what to do, just not how to do it exactly.

Thanks, and hope to get some help soon.

Re: Help! Reading in binary file C++

Posted: Fri Mar 12, 2010 1:02 am
by qpHalcy0n
Usually with those kinds of formats, the preliminary information you've got there will be packed in a header.

The C-style file i/o "fread" will automatically set the file pointer to += however many bytes you just read.

Generally, everything after the magic sum can be packed into a file header object

Code: Select all

typedef struct
{
      unsigned int          fileLen;       // 4 bytes
      unsigned short       nTilesX;      // 2 bytes
      unsigned short       nTilesY;      // 2 bytes
}s_header;

typedef struct
{
      unsigned char        tileDat[3];      // 3 bytes
}s_tile;




// stuff stuff stuff //
// Read up on how to open a file i/o stream (stdio.h) //
FILE* fileObj = NULL;

// "rb" is a mode argument to fopen which specifies the file as "read only" and "binary" //
if((fileObj = fopen("someDir/somefile.map", "rb")) == NULL)
        return false;      // GTFO, file does not exist.

// Read magic number //
unsigned int magic = 0;
fread(&magic, 4, 1, fileObject);

// Read header //
s_header fileHeader;
memset(&fileHead, 0, sizeof(s_header));
fread(&fileHeader, sizeof(s_header), 1, fileObject);

// Now depending on what in the hell file Length actually IS //
// (size including magic/header, size AFTER header...what?).....if you even really need it //
// We can read in the tiles //
unsigned int nTiles = fileHeader.nTilesX * fileHeader.nTilesY;
unsigned int tileBufSize = nTiles * sizeof(s_tile);
s_tile* tileSheet = (s_tile*)malloc(tileBufSize);  // Allocates 3 bytes * x tiles * y tiles //

// Now just read it //
fread(tileSheet, tileBufSize, 1, fileObject);


// Access your tiles by indexing into the array properly //
// This is easy linear offset addressing //


Re: Help! Reading in binary file C++

Posted: Fri Mar 12, 2010 5:15 am
by K-Bal

Re: Help! Reading in binary file C++

Posted: Fri Mar 12, 2010 7:18 am
by RyanPridgeon
-snip-

Re: Help! Reading in binary file C++

Posted: Fri Mar 12, 2010 3:43 pm
by LeonBlade
K-Bal wrote:http://www.cplusplus.com/doc/tutorial/files/

See "Binary files" ;)
I was on that page too :nono: why didn't I see it?!
Hmm... maybe it's because I was doing this at 4 in the morning :lol:

Thank you all.