Advertisement

Reading from a Binary File

Started by November 07, 2000 02:38 PM
12 comments, last by ThaUnknownProgga 24 years, 2 months ago
I''m trying to load a Quake2 map file but i''m not getting very far. The first 4 bytes of the file are "IBSP", so i try to do this:

FILE *fp;
fp = fopen("demo1.bsp", "rb");
if(fp!=NULL)
{
	char *ibsp;
	fread((char *)&ibsp, 4, 1, fp);
	if(strcmp(ibsp,"IBSP")!=0)
	{
		fclose(fp);
		return false;
	}
		
	fclose(fp);
	return true;
}
 
but that always returns false...any help? and info on binary files in general? on quake 2 stuff?
Well, you might want to change your declaration of ibsp to->

char ibsp[4];

and change your fread call to->

fread(ibsp, 4, 1, fp);

That should allow your program to run without doing nasty things to your computers memory.

You should probably study up a bit on pointers before you get too far into this project. You were trying to read the file into memory that you had not yet allocated. What's worse, you were trying to read the file into the memory allocated to store the address of the memory which you had not yet allocated

Edited by - novalis on November 7, 2000 3:47:36 PM
If a man is talking in the forest, and there is no woman there to hear him, is he still wrong?
Advertisement
Alright, that stopped the program from crashing, so now that function just fails. why?
Well, duh, ibsp doesn''t end with a null, right?
Use memcmp instead, or add the extra ''\0'' at the end of string.

"Paranoia is the belief in a hidden order behind the visible." - Anonymous
Ok, now I need to read an unsigned 32 bit integer...first of all, what is this? a float? a long? double? and how can I read this from the file?
hello?
Advertisement
That is an unsigned int
Hmm I thought it was a DWORD...well how do i read it from a binary file stream? is there a specific fscanf code?
Well, you're both kind of wrong and kind of right.

There is no such thing as a DWORD unless you define it. For instance, the windows include file defines a DWORD type.

An unsigned int is not guaranteed to be a specific size. In fact, it is supposed to be the "most convenient" size for the processer that the compiler is building for.

The way to guarantee 32 bits of unsigned storage space in one variable is to declare it to be of type unsigned long. The C language guarantees a long to be 32 bits.

Now, to read it from a binary file, try this...

unsigned long i;
FILE *f;
.
.
.
fread(&i, 4, 1, f);


PS: Good call Staffan... I totaly missed that

Edited by - novalis on November 9, 2000 9:33:02 AM
If a man is talking in the forest, and there is no woman there to hear him, is he still wrong?
Thats not right either. An unsigned long is guaranted to be at least 32 bits. But it can be more. Consider a 64 bit processor, where int probably would be 64bit, then long must be at least 64bit. A dword, when defined will always be 32bit.

This topic is closed to new replies.

Advertisement