Why: Files#

class File#

File I/O wrapper providing RAII-style file handle management.

Public Functions

std::expected<std::size_t, OSError> read(std::uint8_t *data, std::size_t len)#

Read data from the file

Parameters:
  • data – Buffer to read data into

  • len – Maximum number of bytes to read

Returns:

Number of bytes actually read on success, or OSError on failure

std::expected<std::size_t, OSError> write(const std::uint8_t *data, std::size_t len)#

Write data to the file

Parameters:
  • data – Buffer containing data to write

  • len – Number of bytes to write

Returns:

Number of bytes actually written on success, or OSError on failure

Public Static Functions

static std::expected<File, OSError> open(const std::filesystem::path &path, int flags)#

Open a file with the specified flags

Parameters:
  • path – The filesystem path to the file

  • flags – Bitwise OR of open flags (ReadOnly, WriteOnly, Append, Create, Truncate)

Returns:

File object on success, or OSError on failure

static std::expected<void, OSError> rename(const std::filesystem::path &oldpath, const std::filesystem::path &newpath)#

Rename a file or move it to a different location

Parameters:
  • oldpath – Current path of the file

  • newpath – New path for the file

Returns:

void on success, or OSError on failure

Public Static Attributes

static int ReadOnly = O_RDONLY#

Open file in read-only mode.

static int WriteOnly = O_WRONLY#

Open file in write-only mode.

static int Append = O_APPEND#

Append to end of file on each write.

static int Create = O_CREAT#

Create file if it doesn’t exist.

static int Truncate = O_TRUNC#

Truncate file to zero length if it exists.