Autor Tema: File reading  (Leído 2507 veces)

quasist

  • Newbie
  • *
  • Mensajes: 30
  • Karma: 0
  • let_me_alone();
File reading
« en: Enero 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 :)
Function Int Write_Signature()
Private
Begin
End;

josebita

  • Administrator
  • *****
  • Mensajes: 4039
  • Karma: 257
    • BennuGD Mobile Worklog
Re: File reading
« Respuesta #1 en: Enero 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.

Sandman

  • Sr. Member
  • ****
  • Mensajes: 298
  • Karma: 48
Re: File reading
« Respuesta #2 en: Enero 12, 2010, 12:08:31 am »
-- Sandman

quasist

  • Newbie
  • *
  • Mensajes: 30
  • Karma: 0
  • let_me_alone();
Re: File reading
« Respuesta #3 en: Enero 12, 2010, 12:47:44 am »
Could you help with some code?

I got
int map0[127][127];

and

Código: [Seleccionar]
//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?
« última modificación: Enero 12, 2010, 12:54:03 am por quasist »
Function Int Write_Signature()
Private
Begin
End;

Windgate

  • Hero Member
  • *****
  • Mensajes: 2930
  • Karma: 124
    • TRINIT Asociación de Informáticos de Zaragoza
Re: File reading
« Respuesta #4 en: Enero 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)?
Iván García Subero. Programador, profesor de informática, monitor de actividades culturales y presidente de TRINIT Asociación de Informáticos de Zaragoza. http://trinit.es

quasist

  • Newbie
  • *
  • Mensajes: 30
  • Karma: 0
  • let_me_alone();
Re: File reading
« Respuesta #5 en: Enero 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 :)
« última modificación: Enero 12, 2010, 01:48:04 am por quasist »
Function Int Write_Signature()
Private
Begin
End;