From: Dariusz Murakowski Date: Tue, 4 Feb 2014 20:01:52 +0000 (-0500) Subject: Check for file-opening / existence errors (vs segfault). write_mod for general Hamilt... X-Git-Url: http://src.murakowski.org/?a=commitdiff_plain;h=428e220f68b211d6a34396b8f96a409f371ca9a5;p=VirEvoDyn.git Check for file-opening / existence errors (vs segfault). write_mod for general Hamiltonian. --help flag. --- diff --git a/hamiltonian.cpp b/hamiltonian.cpp index 593d9b0..b76615d 100644 --- a/hamiltonian.cpp +++ b/hamiltonian.cpp @@ -18,7 +18,8 @@ Hamiltonian::Hamiltonian(std::string &FILENAME) { this->bJ=1.0; FILE* input; - input = fopen(FILENAME.c_str(),"r"); + input = fopen(FILENAME.c_str(),"r"); // .j + if (input == NULL) { perror((std::string("ERROR: ") + FILENAME).c_str()); exit(1); } typedef std::vector Key; std::map JIndex; @@ -111,7 +112,8 @@ EpitopeHamiltonian::EpitopeHamiltonian(std::string &FILENAME) { this->bJ=1.0; FILE* input; - input = fopen(FILENAME.c_str(),"r"); + input = fopen(FILENAME.c_str(),"r"); // .j + if (input == NULL) { perror((std::string("ERROR: ") + FILENAME).c_str()); exit(1); } typedef std::vector Key; std::map JIndex; @@ -325,6 +327,7 @@ TwoSiteHamiltonian::TwoSiteHamiltonian(std::string &FILENAME) { FILE* input; input = fopen(FILENAME.c_str(),"r"); + if (input == NULL) { perror((std::string("ERROR: ") + FILENAME).c_str()); exit(1); } typedef std::vector Key; std::map JIndex; diff --git a/wf.cpp b/wf.cpp index fa6e343..71ecb24 100644 --- a/wf.cpp +++ b/wf.cpp @@ -49,6 +49,8 @@ void run(RunParameters &r, unsigned seed) { r.setFiles(); FILE *popout=fopen(r.trajectoryOutfile.c_str(),"w"); // .dat FILE *supout=r.useTwoSite ? NULL : fopen(r.supplementaryOutfile.c_str(),"w"); // .sum + if (popout == NULL) { perror("File error"); exit(1); } + if (r.useTwoSite && supout == NULL) { perror("File error"); exit(1); } if (r.importState) importState(r); // .st @@ -103,7 +105,10 @@ void run(RunParameters &r, unsigned seed) { for (i=0; i.st + if (input == NULL) { perror((std::string("ERROR in importState: ") + r.stateInfile).c_str()); exit(1); } char o; double frac; @@ -326,7 +332,7 @@ int main(int argc, char *argv[]) { else if (strcmp(argv[i],"-write_mod")==0) { if (++i==argc) break; else r.write_mod=(unsigned int)strtodouble(argv[i]); } - else if (strcmp(argv[i],"-h")==0) { usage(); return 0; } + else if (strcmp(argv[i],"-h")==0 || strcmp(argv[i],"--help")==0) { usage(); return 0; } else printf("Unrecognized command! '%s'\n",argv[i]);