write struct as raw data to file
Steven Schveighoffer
schveiguy at gmail.com
Sun Sep 26 17:54:38 UTC 2021
On 9/26/21 11:09 AM, Paul wrote:
> I'm building a binary file. I can write my 'short[] myArray' directly
> to the file using: File f = File( "myFile.wav", "wb" );
> f.rawWrite(myArray); It doesn't write any array formatting stuff (i.e.
> '[ , , ]'); it just moves the data into myFile like I want.
>
> I can't seem to do this with myStruct? myStruct is just a declaration
> and initialization of a dozen atomic types (e.g uint WAVE_Id =
> 0x45_56_41_57). When I issue f.rawWrite(myStruct), it writes
> "myStruct(...,...,etc. ) instead of just writing the data.
What is happening is that myStruct must have an alias-this to an array,
and it is writing this array.
That's the only reasonable explanation I can think of.
>
> Is there way to write the myStruct data to the file in a single
> statement...or two?
```d
f.rawWrite((&myStruct)[0 .. 1]);
```
This will change the `myStruct` into an array of one struct instance,
and then it can deal with it.
-Steve
More information about the Digitalmars-d-learn
mailing list