Skip to content
Snippets Groups Projects
Commit fe97fbb9 authored by Toni G's avatar Toni G Committed by Giovanni Bussi
Browse files

Fix in backup output files

(thanks to Toni Giorgino)

I fixed a missing include.

Original comment:
Fixes in backup output files - cant rename open file under windows - print reason for failure - remove leak of FILE*s

(cherry picked from commit 448e84fc)
parent e1accf0f
No related branches found
No related tags found
No related merge requests found
...@@ -32,6 +32,7 @@ ...@@ -32,6 +32,7 @@
#include <iostream> #include <iostream>
#include <string> #include <string>
#include <cstdlib> #include <cstdlib>
#include <cerrno>
namespace PLMD{ namespace PLMD{
...@@ -240,6 +241,7 @@ void OFile::backupFile( const std::string& bstring, const std::string& fname ){ ...@@ -240,6 +241,7 @@ void OFile::backupFile( const std::string& bstring, const std::string& fname ){
FILE* ff=std::fopen(const_cast<char*>(fname.c_str()),"r"); FILE* ff=std::fopen(const_cast<char*>(fname.c_str()),"r");
FILE* fff=NULL; FILE* fff=NULL;
if(ff){ if(ff){
std::fclose(ff);
std::string backup; std::string backup;
size_t found=fname.find_last_of("/\\"); size_t found=fname.find_last_of("/\\");
std::string directory=fname.substr(0,found+1); std::string directory=fname.substr(0,found+1);
...@@ -251,12 +253,11 @@ void OFile::backupFile( const std::string& bstring, const std::string& fname ){ ...@@ -251,12 +253,11 @@ void OFile::backupFile( const std::string& bstring, const std::string& fname ){
backup=directory+bstring +"."+num+"."+file; backup=directory+bstring +"."+num+"."+file;
fff=std::fopen(backup.c_str(),"r"); fff=std::fopen(backup.c_str(),"r");
if(!fff) break; if(!fff) break;
else std::fclose(fff);
} }
int check=rename(fname.c_str(),backup.c_str()); int check=rename(fname.c_str(),backup.c_str());
plumed_massert(check==0,"renaming "+fname+" into "+backup+" failed for some reason"); plumed_massert(check==0,"renaming "+fname+" into "+backup+" failed for reason: "+strerror(errno));
} }
if(ff) std::fclose(ff);
if(fff) std::fclose(fff);
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment