Binary File IO in C

Started by
3 comments, last by Erondial 18 years, 3 months ago
long int filenamesize = strlen(filename);
FILE* filetemp = fopen(filename,"rb");
long int filesize = GetFileSize(filetemp);
long int resourcesize = GetFileSize(resourcefile);
char* buffer = (char*)malloc(filesize);
fread(buffer,1,filesize,filetemp);
Now for some reason, I get a blank buffer when I'm doing this. Anyone have any idea why? I'm trying to open a totally red (255,0,0) 196x196 bitmap, but the buffer's empty. Any idea why? EDIT: And the file is open, it gets the file size properly as something around 115000 bytes or something
Advertisement
My first instinct is that GetFileSize() is returning 0 or a negative value, so you're not actually reading anything in. Otherwise, it should work.
Check all your return values.
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
Nope, it works, I printed it to the console, and it is the correct size.
Waiiiiiiiiiiiiiiit. Got it. GetFileSize seeks to the end of the file and doesn't reuturn the stream to it's previous position, so it was trying to read off the end of the file. My mistake.

This topic is closed to new replies.

Advertisement