Reading a structured binary file?
Jesse Phillips
Jesse.K.Phillips+D at gmail.com
Mon Aug 5 21:48:12 PDT 2013
On Saturday, 3 August 2013 at 18:23:58 UTC, Gary Willoughby wrote:
> On Saturday, 3 August 2013 at 18:14:47 UTC, Gary Willoughby
> wrote:
>> This sounds a great idea but once the file has been opened as
>> a MmFile how to i convert this to a ubyte[] so the
>> std.bitmanip functions work with it?
>
> I'm currently doing this:
>
> auto file = new MmFile("file.dat");
> ubyte[] buffer = cast(ubyte[])file[];
> buffer.read!uint(); //etc.
>
> Is this how you would recommend?
You will need to slice the size of the data you want, otherwise
you're effectively doing std.file.read(). It doesn't need to be
for a single value (as in the example), it could be a block of
data which is then individual parsed for the pieces.
auto file = new MmFile("file.dat");
ubyte[] buffer = cast(ubyte[])file[indexInFile..uint.sizeof];
indexInFile += uint.sizeof;
buffer.read!uint(); //etc.
The only way I'm seeing to advance through the file is to keep an
index on where you're currently reading from. This actually works
perfect for the FileRange I mentioned in the previous post.
Though I'm not familiar with how mmfile manages its memory, but
hopefully there isn't buffer reuse or storing the slice could be
overridden (not an issue for value data, but string data).
More information about the Digitalmars-d-learn
mailing list