file_wrapper.hpp (586B)
1 #ifndef GUARD_RECUMBENTLY_ABRAM_FRAME_MISREGARDS_9196 2 #define GUARD_RECUMBENTLY_ABRAM_FRAME_MISREGARDS_9196 3 #pragma once 4 5 #include <cstdio> 6 #include <memory> 7 #include <stdexcept> 8 9 class File 10 { 11 public: 12 File() noexcept = default; 13 File(FILE* f) noexcept : file{f} {} 14 File(const char* fname, const char* mode) : file{fopen(fname, mode)} 15 { if (!file) throw std::runtime_error("Failed to open file"); } 16 17 operator FILE*() noexcept { return file.get(); } 18 19 private: 20 struct Fclose { void operator()(FILE* f) noexcept { fclose(f); } }; 21 std::unique_ptr<FILE, Fclose> file; 22 }; 23 24 #endif