Using C's fread/fwrite with File objects

John Colvin via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Oct 22 11:33:40 PDT 2015


On Thursday, 22 October 2015 at 18:20:07 UTC, pineapple wrote:
> I'd like to use fread and fwrite in place of File.rawRead and 
> File.rawWrite which force the creation of an array where I'd 
> rather specify a buffer location and length.

D's arrays *are* just buffer locations and lengths with a few 
extra properties, methods and operators.

T* ptrToBuffer = /* ... */;
size_t lengthOfBuffer = /* ... */;
T[] buffer = ptrToBuffer[0 .. lengthOfBuffer];
File f = /* ... */;
T[] filledPartOfBuffer = f.rawRead(buffer);

Note that at no point in the above is a new buffer allocated.


More information about the Digitalmars-d-learn mailing list