74 :
file_(std::exchange(obj.file_,
nullptr)),
76 std::exchange(obj.close_on_destruction_,
false)) {}
100 std::swap(
file_, obj.file_);
110 void open(
const std::string& filepath,
const char* mode) {
113 file_ = std::fopen(filepath.c_str(), mode);
114 if (
file_ ==
nullptr) {
116 "Failed to open {} with mode \"{}\"", filepath, mode));
142 (void)std::fclose(
file_);
154 if (
file_ ==
nullptr) [[unlikely]] {
155 throw file_error(
"Failed to write to file: file is not opened.");
159 const std::size_t written_size =
160 std::fwrite(data.data(), 1, data.size(),
file_);
161 if (written_size != data.size()) [[unlikely]] {
170 if (
file_ ==
nullptr) [[unlikely]] {
171 throw file_error(
"Failed to write to file: file is not opened.");
175 const int result = std::fflush(
file_);
176 if (result != 0) [[unlikely]] {
186 [[nodiscard]]
auto file() const noexcept ->
std::FILE* {
return file_; }
Class of exception on errors in files.
Class to wrap file pointer.
auto operator=(file_wrapper &&obj) noexcept -> file_wrapper &
Move assignment operator.
auto file() const noexcept -> std::FILE *
Get the file pointer.
void set_stderr()
Set this file to standard error.
void set_stdout()
Set this file to standard output.
void swap(file_wrapper &obj) noexcept
Swap with another object.
void write(std::string_view data)
Write data.
file_wrapper(const std::string &filepath, const char *mode)
Constructor to open a file.
file_wrapper(file_wrapper &&obj) noexcept
Move constructor.
std::FILE * file_
File pointer.
void open(const std::string &filepath, const char *mode)
Open a file.
void flush()
Flush buffer.
void close() noexcept
Close this file.
file_wrapper() noexcept=default
Constructor.
~file_wrapper() noexcept
Destructor.
bool close_on_destruction_
Whether to close the file when destructed.
Definition of exceptions.
auto format_errno(fmt::format_string< Args... > format, Args &&... args) -> std::string
Format a message with error message determined by errno.