reading a structure (eg header info) from file

Rikki Cattermole alphaglosined at gmail.com
Wed Jul 3 20:07:16 PDT 2013


On Wednesday, 3 July 2013 at 20:37:24 UTC, captaindet wrote:
> hi,
>
> whilst converting some of my C code into D i got stuck.
>
> in C:
>
> typedef struct { /* info */ } INFO;
> INFO info;
> size_t checkio;
> // read INFO from data file:
> pf_datafile = fopen("datafile","rb");
> checkio = fread((char *) &info, sizeof(info), 1, pf_datafile);
>
> how do i do this in D? i'd like to avoid falling back to 
> calling C functions, but i cannot figure out how do this with 
> phobos functions. mind you, eventually the INFO struct might be 
> anywhere in the file, not only at the beginning.
>
> cheers,
>
> det

Take a look at http://dlang.org/phobos/std_file.html#.read
There is also http://dlang.org/phobos/std_file.html#.readText if 
you wish to read a whole file as text.

When I tested it things like string didn't work but numbers did.
Maybe someone else here can help you if you need that.


More information about the Digitalmars-d-learn mailing list