From bb3b70ad09b51e92570fca3736330c55c733d9c1 Mon Sep 17 00:00:00 2001 From: "Dr. Patrick Urbanke" Date: Sat, 6 Dec 2025 14:02:04 +0100 Subject: [PATCH] Check whether files could be opened before writing; resolves #542 --- include/rfl/io/save_bytes.hpp | 3 +++ include/rfl/io/save_string.hpp | 3 +++ 2 files changed, 6 insertions(+) diff --git a/include/rfl/io/save_bytes.hpp b/include/rfl/io/save_bytes.hpp index 0c5fc208..868a0cda 100644 --- a/include/rfl/io/save_bytes.hpp +++ b/include/rfl/io/save_bytes.hpp @@ -15,6 +15,9 @@ Result save_bytes(const std::string& _fname, const T& _obj, const WriteFunction& _write) { try { std::ofstream output(_fname, std::ios::out | std::ios::binary); + if (!output.is_open()) { + return error("Unable to open file '" + _fname + "' for writing."); + } _write(_obj, output); output.close(); } catch (std::exception& e) { diff --git a/include/rfl/io/save_string.hpp b/include/rfl/io/save_string.hpp index f7c7e6e3..8b19198d 100644 --- a/include/rfl/io/save_string.hpp +++ b/include/rfl/io/save_string.hpp @@ -14,6 +14,9 @@ Result save_string(const std::string& _fname, const T& _obj, const WriteFunction& _write) { try { std::ofstream outfile; + if (!outfile.is_open()) { + return error("Unable to open file '" + _fname + "' for writing."); + } outfile.open(_fname); _write(_obj, outfile); outfile.close();