From d86eed84560410b24130c50b2f20b8652dc72805 Mon Sep 17 00:00:00 2001 From: Giovanni Bussi <giovanni.bussi@gmail.com> Date: Wed, 9 Oct 2013 11:39:21 +0200 Subject: [PATCH] Addresses #40 It just prints a meaningful error. --- src/cltools/SimpleMD.cpp | 8 ++++++++ src/core/CLTool.cpp | 8 +++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/cltools/SimpleMD.cpp b/src/cltools/SimpleMD.cpp index 62fb7f980..224134656 100644 --- a/src/cltools/SimpleMD.cpp +++ b/src/cltools/SimpleMD.cpp @@ -190,6 +190,10 @@ read_input(double& temperature, void read_natoms(const string & inputfile,int & natoms){ // read the number of atoms in file "input.xyz" FILE* fp=fopen(inputfile.c_str(),"r"); + if(!fp){ + fprintf(stderr,"ERROR: file %s not found\n",inputfile.c_str()); + exit(1); + } fscanf(fp,"%1000d",&natoms); fclose(fp); } @@ -198,6 +202,10 @@ void read_positions(const string& inputfile,int natoms,vector<Vector>& positions // read positions and cell from a file called inputfile // natoms (input variable) and number of atoms in the file should be consistent FILE* fp=fopen(inputfile.c_str(),"r"); + if(!fp){ + fprintf(stderr,"ERROR: file %s not found\n",inputfile.c_str()); + exit(1); + } char buffer[256]; char atomname[256]; fgets(buffer,256,fp); diff --git a/src/core/CLTool.cpp b/src/core/CLTool.cpp index 30e2d776c..1b871643d 100644 --- a/src/core/CLTool.cpp +++ b/src/core/CLTool.cpp @@ -157,7 +157,13 @@ bool CLTool::readInputFile( int argc, char**argv, FILE* in, FILE*out ){ } FILE* mystdin=in; - if(argc==2) mystdin=fopen(argv[1],"r"); + if(argc==2){ + mystdin=fopen(argv[1],"r"); + if(!mystdin){ + fprintf(stderr,"ERROR: cannot open file %s\n",argv[1]); + return false; + } + } char buffer[256]; std::string line; line.resize(256); while(fgets(buffer,256,mystdin)){ -- GitLab