Bennu Game Development

English Forums => Helpdesk => Topic started by: quasist on January 11, 2010, 11:32:03 PM

Title: File reading
Post by: quasist on January 11, 2010, 11:32:03 PM
How to read binary files?

For instance there a "data\file.bin" and I need to read byte after byte from it?

Thanks in general. Just exporting level map to *.inc file is somewhat funny to me :)
Title: Re: File reading
Post by: josebita on January 11, 2010, 11:59:53 PM
You can try this:
http://wiki.bennugd.org/index.php?title=Save
I'm not sure if that's what you need, though.
Title: Re: File reading
Post by: Sandman on January 12, 2010, 12:08:31 AM
You want:
Title: Re: File reading
Post by: quasist on January 12, 2010, 12:47:44 AM
Could you help with some code?

I got
int map0[127][127];

and

//open file ????
handle=fopen(loadpath,O_READ);
for (yy=0;yy<128;yy++)
for (xx=0;xx<128;xx++)
// how to read one byte ????
fread(handle,map0[xx][yy]);
end;
end;
//release file ????
fclose(handle);  

Am I right?

But it crashed bennu...

So I suppose I have to load file contents to the memory, only then ripe bytes from it?
Title: Re: File reading
Post by: Windgate on January 12, 2010, 01:14:12 AM
INT fread ( <VOID POINTER data_pos> , <INT length> , <INT filehandle> )

With that you can set on length the number of bytes to read, 1 in your case.

The example you wrote reads int (4 bytes). Anyway it looks good, it must work reading integers from the file... ¿Does the file have enough bytes to read (128x128x4 bytes before end of file :P)?
Title: Re: File reading
Post by: quasist on January 12, 2010, 01:42:25 AM
160kb world in file = 400kb inc file = my DCB file is 2Mb :P

Switched map data from int to byte (I will not draw so much tiles anyway) and all worked :)