digitalmars.D.learn - fread return value?
- Andy Valencia (22/22) Jul 30 Scratching my head here. fread() really appears to be the
- monkyyy (6/29) Jul 30 in code I have running its used as a count and has to be greater
- 0xEAB (7/9) Jul 30 AFAICT your code is wrong.
- Andy Valencia (5/14) Jul 30 (Looks at manual. Looks at code. SMH.)
- =?UTF-8?Q?Ali_=C3=87ehreli?= (24/25) Jul 30 I haven't used anything other than rawRead() for that purpose. rawRead()...
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(); } } ```
Jul 30
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 0fread(&buf[0], buf.length, 1, f);from mine: `auto ret=fread(&data[length],1,data.length-length,ptr);` youve swizzled the arguments
Jul 30
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.
Jul 30
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:(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! Andybut 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.
Jul 30
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
Jul 30