From b0157a7fc86db076b02c0c9c4f03ff5c44fa9777 Mon Sep 17 00:00:00 2001 From: Dmitri Naumov <github@naumov.de> Date: Sat, 7 Jul 2018 14:36:52 +0200 Subject: [PATCH] [BL] FileTools; Add createBinaryFile with check. --- BaseLib/FileTools.cpp | 11 +++++++++++ BaseLib/FileTools.h | 7 +++++++ 2 files changed, 18 insertions(+) diff --git a/BaseLib/FileTools.cpp b/BaseLib/FileTools.cpp index 7ed7c38676a..b9c4eb15ada 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 791736beea6..fd328eb0075 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 * -- GitLab