1#ifndef BYTEME_RAW_FILE_WRITER_HPP
2#define BYTEME_RAW_FILE_WRITER_HPP
9#include "SelfClosingFILE.hpp"
31 RawFileWriter(
const char* path,
size_t buffer_size = 65536) : my_file(path,
"wb") {
32 if (std::setvbuf(my_file.handle,
nullptr, _IOFBF, buffer_size)) {
33 throw std::runtime_error(
"failed to set a buffer size for file writing");
46 void write(
const unsigned char* buffer,
size_t n) {
47 size_t ok = std::fwrite(buffer,
sizeof(
unsigned char), n, my_file.handle);
49 throw std::runtime_error(
"failed to write raw binary file (fwrite error " + std::to_string(std::ferror(my_file.handle)) +
")");
54 if (std::fclose(my_file.handle)) {
55 throw std::runtime_error(
"failed to close raw binary file");
57 my_file.handle =
nullptr;
61 SelfClosingFILE my_file;
Write bytes to a file.
Definition RawFileWriter.hpp:25
RawFileWriter(const std::string &path, size_t buffer_size=65536)
Definition RawFileWriter.hpp:41
void finish()
Definition RawFileWriter.hpp:53
void write(const unsigned char *buffer, size_t n)
Definition RawFileWriter.hpp:46
RawFileWriter(const char *path, size_t buffer_size=65536)
Definition RawFileWriter.hpp:31
Virtual class for writing bytes to a sink.
Definition Writer.hpp:18
virtual void write(const unsigned char *buffer, size_t n)=0
Simple byte readers and writers.