reading a structure (eg header info) from file

Jonathan M Davis jmdavisProg at gmx.com
Wed Jul 3 21:31:34 PDT 2013


On Wednesday, July 03, 2013 15:37:28 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.

You should probably look at std.stdio.File. It looks like its readf function 
might be what you're looking for. If not, there are quite a few functions on 
there, and it's likely that you could get one of them to do what you want.

Or you could always use std.mmfile.MmFile and operate on the file as an array 
and do it all that way (memory-mapped I/O is one of the coolest things ever 
IMHO).

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list