Skip to content
FileReadAndClose

FileReadAndClose

Description

Slurp a file from disk in one call: open, read-to-EOF into out, close. out may be a Buf * (binary) or Str * (text). Removes the open/read/close ceremony that’s nearly identical across every parser caller. The fast path inside file_read_to_{buf,str} (one- shot reserve via FileSeek(END)) still applies.

Parameters

Name Direction Description
path in Path to open. Str * / char * (NUL-terminated).
out out Already-init’d Buf * or Str *. Existing content is overwritten; the destination’s allocator drives growth.

Success

Returns bytes loaded (>= 0); file is closed.

Failure

Returns -1; file is closed; out may be in a partial state – caller should BufDeinit / StrDeinit if it was a fresh container.

Usage example (Cross-references)

Usage examples (Cross-references)
        }
        Buf data = BufInit(alloc);
        if (FileReadAndClose(path, &data) < 0) {
            BufDeinit(&data);
            LOG_ERROR("PeOpen: failed to read {}", path);
        }
        Buf data = BufInit(alloc);
        if (FileReadAndClose(path, &data) < 0) {
            BufDeinit(&data);
            LOG_ERROR("PdbOpen: failed to read {}", path);
        StrDeinit(&response->body);
        response->body = StrInit(response->allocator);
        if (FileReadAndClose(filepath, &response->body) < 0) {
            LOG_ERROR("failed to read file: {}", filepath);
            return NULL;
        }
        Buf data = BufInit(alloc);
        if (FileReadAndClose(path, &data) < 0) {
            BufDeinit(&data);
            LOG_ERROR("ElfOpen: failed to read {}", path);
        }
        Buf data = BufInit(alloc);
        if (FileReadAndClose(path, &data) < 0) {
            BufDeinit(&data);
            LOG_ERROR("MachoOpen: failed to read {}", path);
Last updated on