99#include < exception>
1010#include < filesystem>
1111#include < ios>
12+ #include < iostream>
1213#include < istream>
1314#include < memory>
1415#include < ostream>
@@ -95,6 +96,19 @@ OutStreamHandler::OutStreamHandler(const char* filename, bool binary)
9596}
9697
9798OutStreamHandler::~OutStreamHandler ()
99+ {
100+ try {
101+ close ();
102+ } catch (const std::exception& e) {
103+ std::cerr << " Error: close for " << filename_ << " failed: " << e.what ()
104+ << " \n " ;
105+ } catch (...) {
106+ std::cerr << " Error: close for " << filename_
107+ << " failed with unknown error\n " ;
108+ }
109+ }
110+
111+ void OutStreamHandler::close ()
98112{
99113 if (stream_) {
100114 boost::iostreams::close (*buf_);
@@ -105,9 +119,9 @@ OutStreamHandler::~OutStreamHandler()
105119 if (os_.is_open ()) {
106120 // Any pending output sequence is written to the file.
107121 os_.close ();
122+ // If filename_ exists it will be overwritten
123+ fs::rename (tmp_filename_, filename_);
108124 }
109- // If filename_ exists it will be overwritten
110- fs::rename (tmp_filename_, filename_);
111125}
112126
113127std::ostream& OutStreamHandler::getStream ()
@@ -144,6 +158,19 @@ InStreamHandler::InStreamHandler(const char* filename, bool binary)
144158}
145159
146160InStreamHandler::~InStreamHandler ()
161+ {
162+ try {
163+ close ();
164+ } catch (const std::exception& e) {
165+ std::cerr << " Error: close for " << filename_ << " failed: " << e.what ()
166+ << " \n " ;
167+ } catch (...) {
168+ std::cerr << " Error: close for " << filename_
169+ << " failed with unknown error\n " ;
170+ }
171+ }
172+
173+ void InStreamHandler::close ()
147174{
148175 if (stream_) {
149176 boost::iostreams::close (*buf_);
@@ -152,7 +179,6 @@ InStreamHandler::~InStreamHandler()
152179 }
153180
154181 if (is_.is_open ()) {
155- // Any pending output sequence is written to the file.
156182 is_.close ();
157183 }
158184}
@@ -177,13 +203,30 @@ FileHandler::FileHandler(const char* filename, bool binary)
177203}
178204
179205FileHandler::~FileHandler ()
206+ {
207+ try {
208+ close ();
209+ } catch (const std::exception& e) {
210+ std::cerr << " Error: close for " << filename_ << " failed: " << e.what ()
211+ << " \n " ;
212+ } catch (...) {
213+ std::cerr << " Error: close for " << filename_
214+ << " failed with unknown error\n " ;
215+ }
216+ }
217+
218+ void FileHandler::close ()
180219{
181220 if (file_) {
182- // Any unwritten buffered data are flushed to the OS.
183- std::fclose (file_);
221+ const bool failed = (std::fclose (file_) != 0 );
222+ file_ = nullptr ;
223+ if (failed) {
224+ throw std::runtime_error (" fclose failed for " + tmp_filename_ + " : "
225+ + strerror (errno));
226+ }
227+ // If filename_ exists it will be overwritten.
228+ fs::rename (tmp_filename_, filename_);
184229 }
185- // If filename_ exists it will be overwritten.
186- fs::rename (tmp_filename_, filename_);
187230}
188231
189232FILE* FileHandler::getFile ()
0 commit comments