Check whether files could be opened before writing; resolves #542 (#555)

This commit is contained in:
Dr. Patrick Urbanke (劉自成)
2025-12-06 23:20:38 +01:00
committed by GitHub
parent 27044f7afc
commit 670b42b41a
2 changed files with 6 additions and 0 deletions
+3
View File
@@ -15,6 +15,9 @@ Result<Nothing> 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) {
+3
View File
@@ -15,6 +15,9 @@ Result<Nothing> 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) {