Thread overview
fread return value?
2 days ago
Andy Valencia
2 days ago
monkyyy
2 days ago
0xEAB
2 days ago
Andy Valencia
2 days ago
Ali Çehreli
2 days ago

Scratching my head here. fread() really appears to be the standard C idea of an fread(), so it returns a count. Instead, it returns 0. The buf[] does indeed have the expected content, but I'd like to understand what's up before just changing my code to assume 0 is a success value?

(Linux, ldc2 seen on versions 1.40.0 and 1.41.0)

Thanks,
Andy

import core.stdc.stdio : fopen, fread, fclose;
import std.string : toStringz;

void main(string[] argv) {
    import std.stdio : writeln;

    foreach(a; argv[1 .. $]) {
        auto f = fopen(a.toStringz(), "r".ptr);
        ubyte[1024] buf;
        auto res = fread(&buf[0], buf.length, 1, f);
        writeln(res, ": ", buf);
        f.fclose();
    }
}
2 days ago

On Wednesday, 30 July 2025 at 19:45:20 UTC, Andy Valencia wrote:

>

Scratching my head here. fread() really appears to be the standard C idea of an fread(), so it returns a count. Instead, it returns 0. The buf[] does indeed have the expected content, but I'd like to understand what's up before just changing my code to assume 0 is a success value?

(Linux, ldc2 seen on versions 1.40.0 and 1.41.0)

Thanks,
Andy

import core.stdc.stdio : fopen, fread, fclose;
import std.string : toStringz;

void main(string[] argv) {
    import std.stdio : writeln;

    foreach(a; argv[1 .. $]) {
        auto f = fopen(a.toStringz(), "r".ptr);
        ubyte[1024] buf;
        auto res = fread(&buf[0], buf.length, 1, f);
        writeln(res, ": ", buf);
        f.fclose();
    }
}

in code I have running its used as a count and has to be greater then 0

>

fread(&buf[0], buf.length, 1, f);

from mine: auto ret=fread(&data[length],1,data.length-length,ptr);
youve swizzled the arguments

2 days ago
On Wednesday, 30 July 2025 at 19:45:20 UTC, Andy Valencia wrote:
> but I'd like to understand what's up before just changing my code to assume 0 is a success value?

AFAICT your code is wrong.
The parameters of `fread()` are `ptr`, `size`, `n`, `stream`.
Your code is attempting to read `1` unit with a size of `buf.length`. If the stream has less then a single unit of size `buf.length` left, it has obviously read 0 units. Which it reports.
2 days ago
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

2 days ago
On Wednesday, 30 July 2025 at 20:03:37 UTC, 0xEAB wrote:
> On Wednesday, 30 July 2025 at 19:45:20 UTC, Andy Valencia wrote:
>> but I'd like to understand what's up before just changing my code to assume 0 is a success value?
>
> AFAICT your code is wrong.
> The parameters of `fread()` are `ptr`, `size`, `n`, `stream`.
> Your code is attempting to read `1` unit with a size of `buf.length`. If the stream has less then a single unit of size `buf.length` left, it has obviously read 0 units. Which it reports.

(Looks at manual.  Looks at code.  SMH.)

Thanks.  I've been writing C code since K&R, but I guess it _has_ been a while.  Sorry to bug you for such a dumb one!

Andy