write struct as raw data to file

Vitaliy Fadeev vital.fadeev at gmail.com
Mon Sep 27 05:44:35 UTC 2021


On Sunday, 26 September 2021 at 15:09:38 UTC, Paul wrote:
> Is there way to write the myStruct data to the file in a single 
> statement...or two?
>
> Thanks for any assistance.

         Header[1] header;

         void readFileHeader( ref File f )
         {
             f.rawRead( header );
         }

Such i read raw data from .ttf file.

And will be logically if will to write:

         Header[1] header;

         void writeFileHeader( ref File f )
         {
             f.rawWrite( header );
         }

Declaration rawRead/rawWrite is useful, beauty and logical:

         void rawWrite(T)( in T[] buffer );

Reason for using array for return number of readed bytes:

         auto readed = f.rawRead( header );
         // readed.length
         // readed[0]
         // readed[1]

     And logically and symmetrically use rawWrite() with array:

         Header[1] header;
         f.rawWrite( header );

You can write other implementation:

         void rawWrite( T )( ref FILE f, T data )
         {
             //
         }

Why not ?

     And check the cerealed: 
https://code.dlang.org/packages/cerealed

         struct Foo
         {
             int i;
         }

         const foo = Foo(5);

         f.rawWrite( foo.cerealize ) // is ubyte[]



More information about the Digitalmars-d-learn mailing list