Binary I/O for Newbie

Ali Çehreli acehreli at yahoo.com
Mon Feb 27 10:37:33 PST 2012


On 02/27/2012 10:21 AM, tjb wrote:

 > 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;
 > }

The equivalent of that C++ (and C) struct would be almost the same in D. 
Just replace char with 'ubyte' (or 'byte', if the 'char' were signed). 
Let me stress the fact that character types of D are UTF code units, not 
bytes.

struct TaqIdx {
     ubyte[10] symbol;
     int tdate;
     int begrec;
     int endrec;
}

On the other hand, if symbol were indeed in ASCII, and will be treated 
as such in the program, then char[10] is fine too:

     char[10] symbol;

Also, the int type is always 32 bits in D. Check whether that was the 
case for the system that the C++ TaqIdx was used on.

You can read in binary by std.stdio.rawRead. It is mildly annoying that 
you still have to use an array even when reading a single TaqIdx:

     TaqIdx[1] taqs;
     file.rawRead(taqs);

Then use taqs[0] if there is only one.

 > Can you give some pointers?

Must... resist... :p

Ali



More information about the Digitalmars-d-learn mailing list