Binary I/O for Newbie

Justin Whear justin at economicmodeling.com
Mon Feb 27 10:37:00 PST 2012


On Mon, 27 Feb 2012 19:21:21 +0100, tjb wrote:

> All,
> 
> I am just starting to learn D.  I am an economist - not a programmer, so
> I appreciate your patience with lack of knowledge.
> 
> I have some financial data in a binary file that I would like to
> process. In C++ I have the data in a structure like this:
> 
> struct TaqIdx {
>    char symbol[10];
>    int tdate;
>    int begrec;
>    int endrec;
> }
> 
> And I use an ifstream to cast the data to the structure in read. I'm
> struggling to get a handle on I/O in D.  Can you give some pointers? 
> Thanks so much!
> 
> TJB

Check out std.stream (http://dlang.org/phobos/std_stream.html). I'd do 
something like this:

  auto input = new File("somefile");
  TagIdx tag;
  input.readExact( &tag, TagIdx.sizeof );

If you get funky results, the file might be using a different endianness.


More information about the Digitalmars-d-learn mailing list