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 :)
You can try this:
http://wiki.bennugd.org/index.php?title=Save
I'm not sure if that's what you need, though.
You want:
- fopen() (http://wiki.bennugd.org/index.php?title=Fopen)
- fread() (http://wiki.bennugd.org/index.php?title=Fread)
- fclose() (http://wiki.bennugd.org/index.php?title=Fclose)
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?
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)?
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 :)