class basic_file


class basic_file
    : public seekable_io_resource<std::int64_t>

Description

The basic_file class serves as a base class for subclasses that implement the platform-specific code for managing files. All platform-specific subclasses of basic_file are called simply file so that code that uses them stays the same across all platforms. You generally don't want to use this class directly in application code. Rather, you use the file class to read and write files on all supported platforms.

Public Member Functions

basic_file ()
noexcept

Constructs a basic_file object not associated with any file.

basic_file (
    const std::filesystem::path& file_path,
    access_mode mode,
    std::size_t buffer_capacity = io_buffer::default_buffer_capacity
)

Constructs a basic_file object associated with the given file path and buffer capacity. No file is acquired from the operating system, only the path to the file and desired access mode are stored.

Parameters:
  • file_path:

    The file path to store in this basic file.

  • mode:

    The access mode to store in this basic file.

  • buffer_capacity:

    The capacity of the I/O buffer to associate with this basic_file object.

virtual
bool
is_open ()
const noexcept = 0

Returns true if the file is open. Otherwise, returns false.

This functionality is platform-dependent and therefore this function must be overridden in a platform-specific subclass.

access_mode
get_access_mode ()
const noexcept

Returns the access mode associated with this basic file.

bool
is_at_end ()
const noexcept override

Returns true if the file is at its EOF (end-of-file) position. Otherwise, returns false.

virtual
bool
open (
    const std::filesystem::path& file_path,
    access_mode mode,
    if_not_there if_file_not_there
)
noexcept = 0

Opens the file at the given path with the given access mode. The last argument tells what to do if the specified file doesn't already exists, that is, whether to create the file or fail to open it. Returns true if opening the file succeeds. Otherwise, returns false.

This functionality is platform-dependent and therefore this function must be overridden in a platform-specific subclass.

Parameters:
  • file_path:

    The path to the file to open.

  • mode:

    The access mode to open the file in. This tells whether you want to just read, just write or both read and write. This cannot be changed later unless you close the file and reopen it.

  • if_file_not_there:

    The action to take if the specified file does not exist. This can be if_not_there::create, which causes the file to be created, or if_not_there::fail, which causes this function to simply return false without taking any action.

virtual
void
close ()
noexcept = 0

Flushes the I/O buffer and closes the file.

This functionality is platform-dependent and therefore this function must be overridden in a platform-specific subclass.

std::size_t
read (
    std::byte* data,
    std::size_t byte_count
)
override

Reads byte_count bytes from this file and places them in the provided data array.

Parameters:
  • data:

    The array into which the read bytes will be placed.

  • byte_count:

    The number of bytes to read from this file.

std::size_t
write (
    const std::byte* data,
    std::size_t byte_count
)
override

Writes byte_count bytes from the data array to this file.

Parameters:
  • data:

    The array of bytes that will be written to this file.

  • byte_count:

    The number of bytes to write to this file.



Copyright © 2022-2025 Daniel T. McGinnis

CCL was first published in 2022, and is still actively maintained.

This website was first published in 2022, and is still actively maintained.

This specific web page was first published in 2022.