From dd3be93ea1df53df020b89be104adf862cbc263a Mon Sep 17 00:00:00 2001
From: Moritz Maxeiner <mm@ucw.sh>
Date: Wed, 5 Jul 2023 15:33:48 +0200
Subject: [PATCH] Throw exception if output file cannot be opened, try to
 create output file path.

---
 CMakeLists.txt | 2 ++
 src/main.cc    | 5 +++++
 2 files changed, 7 insertions(+)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 1cf4902..f52f4d5 100755
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -8,6 +8,8 @@ add_executable(camera_calibration
 	src/main.cc
 )
 
+set_property(TARGET camera_calibration PROPERTY CXX_STANDARD 17)
+
 find_package(OpenCV 4 QUIET)
 if(NOT OpenCV_FOUND)
     find_package(OpenCV 3 REQUIRED)
diff --git a/src/main.cc b/src/main.cc
index 8d6f185..571872d 100755
--- a/src/main.cc
+++ b/src/main.cc
@@ -5,6 +5,7 @@
 #include <string>
 #include <ctime>
 #include <cstdio>
+#include <filesystem>
 
 #include <opencv2/core.hpp>
 #include <opencv2/core/utility.hpp>
@@ -567,7 +568,11 @@ static void saveCameraParams( Settings& s, Size& imageSize, Mat& cameraMatrix, M
                               const vector<float>& reprojErrs, const vector<vector<Point2f> >& imagePoints,
                               double totalAvgErr )
 {
+    filesystem::create_directories(filesystem::path(s.outputFileName).parent_path());
+
     FileStorage fs( s.outputFileName, FileStorage::WRITE );
+    if (!fs.isOpened())
+        throw std::runtime_error("Could not open camara parameters file for writing");
 
     time_t tm;
     time( &tm );
-- 
GitLab