From 308b4157bc6d0b0f762e7ca559771a505e3ab101 Mon Sep 17 00:00:00 2001 From: ext-calvo_p <pedro.calvo@ciemat.es> Date: Fri, 27 Nov 2020 15:00:33 +0100 Subject: [PATCH] use boost::filesystem to check if the file exists --- src/Distribution/Distribution.cpp | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/src/Distribution/Distribution.cpp b/src/Distribution/Distribution.cpp index 9fcf78a14..3438fe617 100644 --- a/src/Distribution/Distribution.cpp +++ b/src/Distribution/Distribution.cpp @@ -45,6 +45,7 @@ #include <sys/time.h> +#include <boost/filesystem.hpp> #include <boost/regex.hpp> #include <boost/numeric/odeint/stepper/runge_kutta4.hpp> @@ -1001,28 +1002,25 @@ size_t Distribution::getNumberOfParticlesInFile(std::ifstream &inputFile) { void Distribution::createDistributionFromFile(size_t /*numberOfParticles*/, double massIneV) { - *gmsg << level3 << "\n" - << "------------------------------------------------------------------------------------\n"; - *gmsg << "READ INITIAL DISTRIBUTION FROM FILE \"" - << Attributes::getString(itsAttr[Attrib::Distribution::FNAME]) - << "\"\n"; - *gmsg << "------------------------------------------------------------------------------------\n" << endl; - // Data input file is only read by node 0. std::ifstream inputFile; std::string fileName = Attributes::getString(itsAttr[Attrib::Distribution::FNAME]); - bool failedOpen = 0; - if (Ippl::myNode() == 0) { - inputFile.open(fileName.c_str()); - failedOpen = static_cast<bool>(inputFile.fail()); - } - reduce(failedOpen,failedOpen,OpAddAssign()); - if (failedOpen) { + if (!boost::filesystem::exists("fileName")) { throw OpalException("Distribution::createDistributionFromFile", "Open file operation failed, please check if \"" - + fileName - + "\" really exists."); + + fileName + + "\" really exists."); } + if (Ippl::myNode() == 0) { + inputFile.open(fileName.c_str()); + } + + *gmsg << level3 << "\n" + << "------------------------------------------------------------------------------------\n"; + *gmsg << "READ INITIAL DISTRIBUTION FROM FILE \"" + << Attributes::getString(itsAttr[Attrib::Distribution::FNAME]) + << "\"\n"; + *gmsg << "------------------------------------------------------------------------------------\n" << endl; size_t numberOfParticlesRead = getNumberOfParticlesInFile(inputFile); /* -- GitLab