Reading Binary files...

Started by
0 comments, last by Nyarlath 16 years, 6 months ago
Hey... I'm attempting to read in a binary file (.3ds file) and extract the data contained in it. This is the first time I've used binary file so I've got a little bit stuck and was wondering if anyone could help me. this is what i have so far...basically I've just used ifstream to read in the first 2 bytes of the binary file in buffer.

#include <iostream>
#include <fstream>
using namespace std;

unsigned short chunk_id;
unsigned int chunk_length;

char buffer[2];

int main ()
{
	ifstream file ("test.3ds", ios::in|ios::binary);
	if(file.read(buffer, 2))
	{
		cout << "first 2 bytes of file read in" << endl;
	}

	else
	{
		cout << "cannot open file" << endl;
	}

	return 0;
}

what I want to do is to check what the hexadecimal value read into the buffer was. If i just compare them, for example if(buffer = 0x4D4D) then i get an error saying cannot convert from int to char. If anyone could help me i'd be very grateful thanks
Advertisement
You have to test the data where the buffer is pointing to, not the buffer itself (which is an address); and don't confuse = (assignment) and == (test).

-> if (buffer[0] == 0x4D && buffer[1] == 0x4D)

Edit: I might suggest you to google for an already implemented 3ds loader, but the code you will find might be hard to understand.

This topic is closed to new replies.

Advertisement