From 49b062908b9db8dbdae6249b1ad6d487080f4758 Mon Sep 17 00:00:00 2001 From: Amar Mahmutbegovic Date: Sun, 3 Nov 2024 16:00:05 +0100 Subject: [PATCH] check if file is opened in destructor --- Chapter13/lfs_raii/app/src/main_lfs_raii.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/Chapter13/lfs_raii/app/src/main_lfs_raii.cpp b/Chapter13/lfs_raii/app/src/main_lfs_raii.cpp index 3107495..e895bf7 100644 --- a/Chapter13/lfs_raii/app/src/main_lfs_raii.cpp +++ b/Chapter13/lfs_raii/app/src/main_lfs_raii.cpp @@ -16,6 +16,8 @@ namespace fs{ struct lfs { + lfs() = delete; + static inline lfs_t fs_lfs; static void init() { @@ -33,8 +35,10 @@ public: } } ~file() { - printf("Closing file in destructor.\r\n"); - lfs_file_close(&lfs::fs_lfs, &file_); + if(is_open()) { + printf("Closing file in destructor.\r\n"); + lfs_file_close(&lfs::fs_lfs, &file_); + } } [[nodiscard]] bool is_open() const {