fread return value?

monkyyy crazymonkyyy at gmail.com
Wed Jul 30 19:57:56 UTC 2025


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
>
> ```d
> 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


More information about the Digitalmars-d-learn mailing list