#Best way to read a file into a buffer?

1 messages · Page 1 of 1 (latest)

latent dagger
#

I was doing this before with just read(&buf) but found it wasn't very clean. I'd like to use readToEndAlloc() instead and have the following questions:

  1. If I use FixedBufferAllocator for alloc, will it return a buffer that is sized to the same length as the file input?
  2. Does it matter what allocator I use for this?
vale temple
#

not sure what you mean by the first one

#

for the second one, it doesn't matter, no

#

and by the way, you can specify the maximum amount to read from the file

#

so you don't end up allocating bajillions

latent dagger
vale temple
#

yes

#

std.fs.File.seekFromEnd(0);

#

std.fs.File.getPos();

#

should tell you the exact size in bytes of a file's contents

#

that's only after you've opened it though

stuck pollen
#

you can also stat() it

vale temple
#

yes actually I just see that getEndPos() does that

stuck pollen
#

didn't realize there was a dedicated function for that

vale temple
#

you can just do const sizeInBytes = try file.getEndPos(); then

latent dagger
#
    const cwd: std.fs.Dir = std.fs.cwd();
    const file = try cwd.openFile("input.txt", .{});
    defer file.close();
    // const fba = std.heap.FixedBufferAllocator.init(&mem);
    var gpa = std.heap.GeneralPurposeAllocator(.{}){};
    const alloc = gpa.allocator();
    defer gpa.deinit();
    const buf = try file.readToEndAlloc(alloc, try file.getEndPos());

#

It's erroring out on the defer

vale temple
#

well what's the error

#

and there's 2 defers

#

you probably need to do defer _ = gpa.deinit();

#

since I am guessing the error is a compile error for an unused return value

latent dagger
#

yeah that was it

#

it now compiles, but I get a runtime memory leak

vale temple
#

such is the way of using low level code

latent dagger
#

idk what happened though, if the code does what I think it does then the buffer should just be the file contents and nothing more

#

so idk why it would leak memory. maybe I need to free the iterator?

vale temple
#

you need to free the buf

#

defer allocator.free(buf);