Hello everyone, very noob question but I am “loosing myself in a glass of water”.
I am trying to write a system monitor (akin to htop) in zig, to learn zig. “no hidden control flow” is making me humble real fast.
I need to read an arbitrary file at /proc/pid/stat.
So far I understood that I need to use the std.Io.File to read it. As was posted here previously and on google, this post comes up.
It uses cwd() but to my understanding that opens a handle in the directory the executable is running in, while I want to open an arbitrary file path.
Also I am unsure what the right way is. Should I use readalloc or just the read with a big enough buffer.
You can use std.Io.Dir.openFileAbsolute for absolute paths, though if you look at the source code it uses .cwd() as the current directory navigates root paths too.
use a reader, it is an interface for reading bytes from arbitrary places and has a useful API.
You’d want to give the reader a buffer big enough for the largest chunk of data you need at a time, this allows you to leave managing the buffer to the reader and all you need to care about is that reading more invalidates slices into that buffer. If you need such data to persist across reads then you have to copy it to a stable place. But don’t worry about it, in most cases you don’t need to do that.