Sign in to follow this  

Reading Binary files...

This topic is 3721 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

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

This topic is 3721 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this