From fe97fbb97d50893bf9d71185796b973d6f546280 Mon Sep 17 00:00:00 2001
From: Toni G <toni.giorgino@gmail.com>
Date: Thu, 12 Dec 2013 15:56:02 +0100
Subject: [PATCH] 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 448e84fcfdd0a8f6996a8e6f3307444fc45f6497)
---
 src/tools/OFile.cpp | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/src/tools/OFile.cpp b/src/tools/OFile.cpp
index effc8fa18..5b20195fb 100644
--- a/src/tools/OFile.cpp
+++ b/src/tools/OFile.cpp
@@ -32,6 +32,7 @@
 #include <iostream>
 #include <string>
 #include <cstdlib>
+#include <cerrno>
 
 namespace PLMD{
 
@@ -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* fff=NULL;
      if(ff){
+       std::fclose(ff);
        std::string backup;
        size_t found=fname.find_last_of("/\\");
        std::string directory=fname.substr(0,found+1);
@@ -251,12 +253,11 @@ void OFile::backupFile( const std::string& bstring, const std::string& fname ){
          backup=directory+bstring +"."+num+"."+file;
          fff=std::fopen(backup.c_str(),"r");
          if(!fff) break;
+	 else std::fclose(fff);
        }
        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);
    }
 }
 
-- 
GitLab