How to read a C++ class from file into memory

David Finlayson david.p.finlayson at gmail.com
Thu Mar 22 21:42:15 PDT 2007



Daniel Keep Wrote:

> For my money, that's a bad idea because the binary representation of a
> struct or object isn't necessarily the same on different machines or
> even same machine, different operating system.
> 

Agreed, however, the file is a binary storage format for a sonar system. I have been given enough code snippets from the company to read the file (and I have a working Python version), what I want to do now is convert this code to D.

Ultimately, it is my problem to conform to their file format. I don't even know the byte alignment. However, I do have a working Python prototype. (I guess if I thought about it, I could figure the alignment out now).

> The nice thing about Python is the pickle protocol.  That's what I
> assume you were using.  Since Python can interactively inspect objects
> to find out what data is attached to them, this is really easy.  It's
> also nice because pickle isn't blind: it will serialise things in a
> predictable format based on type, not on in-memory layout.
> 

In my case, I used Python's struct.unpack module to build a reader for each of the classes, structs and unions (yes, they used all three types). It took me a while, but I was able to identify where the padding bytes were placed to fill out the structures on disk. So, I understand exactly how the file is stored on disk. All I need to do is learn how to read the file efficiently in D.

Thanks for answering my post. Do you know how I might use std.stream to read these files?

David






More information about the Digitalmars-d-learn mailing list