File module

file_read

procedureSource

Reads up to bytes_to_read bytes from the current position into vdata, and returns (success, total_read).

file_read :: (
    f: File,
    vdata: *void,
    bytes_to_read: s64
) -> (success: bool, total_read: s64)

It keeps reading until the count is met or the end is reached, so a short count with true means the end of the file (or of a pipe whose writer closed), and 0 means it is at the end. A closed file, a null buffer or a count of 0 or less fails with 0 bytes. A read error gives false with the bytes that did arrive. The position moves by total_read.

Parameters

f: File
vdata: *void
bytes_to_read: s64

Returns

success: bool
total_read: s64
Example
#import "Extensions/Environment";
base := environment_variable("TMPDIR");
if base == "" base = environment_variable("TEMP");
if base == "" base = "/tmp";
path := tprint("%/jai-doc-file-read.bin", base);
defer file_delete(path);

assert(write_entire_file(path, "abcdef"));
file, ok := file_open(path);
assert(ok);
defer file_close(*file);

buffer: [4] u8;
success, count := file_read(file, buffer.data, buffer.count);
assert(success && count == 4 && buffer[0] == #char "a");
success, count = file_read(file, buffer.data, buffer.count);
assert(success && count == 2); // short: the end of the file
success, count = file_read(file, buffer.data, buffer.count);
assert(success && count == 0); // at the end

Doesn't run in the browser (only on windows).

See also