Dusk Audio Framework
Loading...
Searching...
No Matches
SafeFileWriter Struct Reference

#include <Filesystem.hpp>

Public Member Functions

 SafeFileWriter (const char *const pathname, const char *const mode="w")
 ~SafeFileWriter ()
bool ok () const noexcept
size_t write (const void *const ptr, const size_t size, const size_t nmemb=1) const

Public Attributes

String filename
FILE *const fd

Detailed Description

Handy class to help write files in a safe way, which does:

  • open pathname + ".tmp" instead of opening a file directly (so partial writes are safe)
  • on close, flush data to disk and rename file to remove ".tmp"

To use it, create a local variable (on the stack) and call ok() or manually check fd variable.

if (const SafeFileWriter file("/path/to/file.txt"); file.ok())
file.write("Success!");
SafeFileWriter(const char *const pathname, const char *const mode="w")
Definition Filesystem.hpp:74

Constructor & Destructor Documentation

◆ SafeFileWriter()

SafeFileWriter::SafeFileWriter ( const char *const pathname,
const char *const mode = "w" )
inline

Constructor, opening pathname + ".tmp" for writing.

◆ ~SafeFileWriter()

SafeFileWriter::~SafeFileWriter ( )
inline

Destructor, will flush file data contents, close and rename file.

Member Function Documentation

◆ ok()

bool SafeFileWriter::ok ( ) const
inlinenoexcept

Check if the file was opened successfully.

◆ write()

size_t SafeFileWriter::write ( const void *const ptr,
const size_t size,
const size_t nmemb = 1 ) const
inline

Wrapper around fwrite, purely for convenience.


The documentation for this struct was generated from the following file: