From 670b42b41ae6062da61a2fa24ce1b70c903d98a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dr=2E=20Patrick=20Urbanke=20=28=E5=8A=89=E8=87=AA=E6=88=90?= =?UTF-8?q?=29?= Date: Sat, 6 Dec 2025 23:20:38 +0100 Subject: [PATCH] Check whether files could be opened before writing; resolves #542 (#555) --- 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..17ee1433 100644 --- a/include/rfl/io/save_string.hpp +++ b/include/rfl/io/save_string.hpp @@ -15,6 +15,9 @@ Result save_string(const std::string& _fname, const T& _obj, try { std::ofstream outfile; outfile.open(_fname); + if (!outfile.is_open()) { + return error("Unable to open file '" + _fname + "' for writing."); + } _write(_obj, outfile); outfile.close(); } catch (std::exception& e) {