fread return value?

Ali Çehreli acehreli at yahoo.com
Wed Jul 30 21:20:03 UTC 2025


On 7/30/25 12:45 PM, Andy Valencia wrote:
 > Scratching my head here.  fread()

I haven't used anything other than rawRead() for that purpose. rawRead() 
returns a slice of what it's just read:

import std.algorithm;
import std.exception;
import std.file;
import std.format;
import std.stdio;

void main(string[] args) {
     enforce(args.length == 2, "Please provide the name of the file to 
read.");

     auto file = File(args[1], "r");
     auto size = getSize(file.name);

     auto buffer = new ubyte[1024 * 1024];
     while (size) {
         const readSize = min(size, buffer.length);
         auto read = file.rawRead(buffer[0..readSize]);
         enforce(read.length == readSize, format!"Read %,s bytes instead 
of %,s."(read.length, readSize));

         writefln!"Read %,s bytes"(read.length);

         size -= read.length;
     }
}

Ali



More information about the Digitalmars-d-learn mailing list