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

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
LeonBlade
Chaos Rift Demigod
Chaos Rift Demigod
Posts: 1314
Joined: Thu Jan 22, 2009 12:22 am
Current Project: Trying to make my first engine in C++ using OGL
Favorite Gaming Platforms: PS3
Programming Language of Choice: C++
Location: Blossvale, NY

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

Post 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.
Last edited by LeonBlade on Fri Mar 12, 2010 9:16 pm, edited 1 time in total.
There's no place like ~/
qpHalcy0n
Respected Programmer
Respected Programmer
Posts: 387
Joined: Fri Dec 19, 2008 3:33 pm
Location: Dallas
Contact:

Re: Help! Reading in binary file C++

Post 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 //

K-Bal
ES Beta Backer
ES Beta Backer
Posts: 701
Joined: Sun Mar 15, 2009 3:21 pm
Location: Germany, Aachen
Contact:

Re: Help! Reading in binary file C++

Post by K-Bal »

User avatar
RyanPridgeon
Chaos Rift Maniac
Chaos Rift Maniac
Posts: 447
Joined: Sun Sep 21, 2008 1:34 pm
Current Project: "Triangle"
Favorite Gaming Platforms: PC
Programming Language of Choice: C/C++
Location: UK
Contact:

Re: Help! Reading in binary file C++

Post by RyanPridgeon »

-snip-
Ryan Pridgeon
C, C++, C#, Java, ActionScript 3, HaXe, PHP, VB.Net, Pascal
Music | Blog
User avatar
LeonBlade
Chaos Rift Demigod
Chaos Rift Demigod
Posts: 1314
Joined: Thu Jan 22, 2009 12:22 am
Current Project: Trying to make my first engine in C++ using OGL
Favorite Gaming Platforms: PS3
Programming Language of Choice: C++
Location: Blossvale, NY

Re: Help! Reading in binary file C++

Post 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.
There's no place like ~/
Post Reply