// -*- c++ -*- // Distributed under the BSD 2-Clause License. // See accompanying file LICENSE for details. #pragma once #include class TmpFile { public: TmpFile(const std::string& data = {}) { auto tmp_dir = std::filesystem::temp_directory_path(); auto tmp_file_template = tmp_dir / "ctor_tmp_file-"; std::FILE* fp{nullptr}; int counter{}; while(!fp) { filename = tmp_file_template.string() + std::to_string(counter++); fp = std::fopen(filename.data(), "wx"); } std::fwrite(data.data(), data.size(), 1, fp); std::fclose(fp); } ~TmpFile() { std::filesystem::remove(filename); } const std::string& get() const { return filename; } private: std::string filename; };