diff --git a/BaseLib/FileTools.cpp b/BaseLib/FileTools.cpp index 7ed7c38676ab0a6532b99ad8aab73964a091ff17..b9c4eb15ada69c52d3774b93b629282041e96de6 100644 --- a/BaseLib/FileTools.cpp +++ b/BaseLib/FileTools.cpp @@ -13,6 +13,7 @@ */ #include "FileTools.h" +#include "Error.h" #include "StringTools.h" #include <sys/stat.h> @@ -56,6 +57,16 @@ void truncateFile( std::string const& filename) ofs.close(); } +std::ofstream createBinaryFile(std::string const& file_name) +{ + std::ofstream os(file_name, std::ios::binary); + if (!os) + { + OGS_FATAL("Could not open file '%s' for output.", file_name.c_str()); + } + return os; +} + namespace { diff --git a/BaseLib/FileTools.h b/BaseLib/FileTools.h index 791736beea60f52c193459104873fb47939aeb69..fd328eb007590b6a507653c71c8200f46e429bb9 100644 --- a/BaseLib/FileTools.h +++ b/BaseLib/FileTools.h @@ -97,6 +97,13 @@ std::vector<T> readBinaryArray(std::string const& filename, std::size_t const n) return std::vector<T>(); } +/** + * Create a binary file for output. + * + * Wrapper around the std::ofstream constructor with error checking. + */ +std::ofstream createBinaryFile(std::string const& file_name); + /** * \brief truncate a file *