From 54a8b4e08bb89f7e42879f8376644ee6a1862c8b Mon Sep 17 00:00:00 2001 From: Axel Kohlmeyer Date: Thu, 4 Jun 2020 19:46:35 -0400 Subject: [PATCH] more local buffers removed and file error status added. --- src/balance.cpp | 6 +++- src/bond.cpp | 6 +++- src/input.cpp | 16 +++++---- src/lammps.cpp | 42 ++++++++++++++++------- src/molecule.cpp | 3 +- src/pair.cpp | 6 +++- src/potential_file_reader.cpp | 4 +-- src/read_data.cpp | 27 ++++++--------- src/read_restart.cpp | 35 ++++++++----------- src/reader.cpp | 20 +++++------ src/universe.cpp | 26 +++++++++------ src/variable.cpp | 63 +++++++++++++---------------------- src/write_coeff.cpp | 16 ++++----- src/write_data.cpp | 10 +++--- src/write_restart.cpp | 18 +++++----- 15 files changed, 152 insertions(+), 146 deletions(-) diff --git a/src/balance.cpp b/src/balance.cpp index c3b6f7d3bf..77d0c29a0c 100644 --- a/src/balance.cpp +++ b/src/balance.cpp @@ -39,6 +39,8 @@ #include "imbalance_var.h" #include "memory.h" #include "error.h" +#include "utils.h" +#include "fmt/format.h" using namespace LAMMPS_NS; @@ -506,7 +508,9 @@ void Balance::options(int iarg, int narg, char **arg) if (outflag && comm->me == 0) { fp = fopen(arg[outarg],"w"); - if (fp == NULL) error->one(FLERR,"Cannot open (fix) balance output file"); + if (fp == NULL) + error->one(FLERR,fmt::format("Cannot open (fix) balance output file {}: {}", + arg[outarg], utils::getsyserror())); } } diff --git a/src/bond.cpp b/src/bond.cpp index d16ce5fe0f..36d809123a 100644 --- a/src/bond.cpp +++ b/src/bond.cpp @@ -21,6 +21,8 @@ #include "atom_masks.h" #include "memory.h" #include "error.h" +#include "utils.h" +#include "fmt/format.h" using namespace LAMMPS_NS; @@ -254,7 +256,9 @@ void Bond::write_file(int narg, char **arg) FILE *fp; if (me == 0) { fp = fopen(arg[4],"a"); - if (fp == NULL) error->one(FLERR,"Cannot open bond_write file"); + if (fp == NULL) + error->one(FLERR,fmt::format("Cannot open bond_write file {}: {}", + arg[4], utils::getsyserror())); } // initialize potentials before evaluating bond potential diff --git a/src/input.cpp b/src/input.cpp index 5d4760149b..76908f9ad1 100644 --- a/src/input.cpp +++ b/src/input.cpp @@ -252,7 +252,8 @@ void Input::file(const char *filename) infile = fopen(filename,"r"); if (infile == NULL) - error->one(FLERR,fmt::format("Cannot open input script {}",filename)); + error->one(FLERR,fmt::format("Cannot open input script {}: {}", + filename, utils::getsyserror())); infiles[nfile++] = infile; } @@ -1036,7 +1037,8 @@ void Input::include() infile = fopen(arg[0],"r"); if (infile == NULL) - error->one(FLERR,fmt::format("Cannot open input script {}",arg[0])); + error->one(FLERR,fmt::format("Cannot open input script {}: {}", + arg[0], utils::getsyserror())); infiles[nfile++] = infile; } @@ -1069,7 +1071,8 @@ void Input::jump() if (infile && infile != stdin) fclose(infile); infile = fopen(arg[0],"r"); if (infile == NULL) - error->one(FLERR,fmt::format("Cannot open input script {}",arg[0])); + error->one(FLERR,fmt::format("Cannot open input script {}: {}", + arg[0], utils::getsyserror())); infiles[nfile-1] = infile; } @@ -1112,7 +1115,8 @@ void Input::log() else logfile = fopen(arg[0],"w"); if (logfile == NULL) - error->one(FLERR,fmt::format("Cannot open logfile {}",arg[0])); + error->one(FLERR,fmt::format("Cannot open logfile {}: {}", + arg[0], utils::getsyserror())); } if (universe->nworlds == 1) universe->ulogfile = logfile; @@ -1189,8 +1193,8 @@ void Input::print() if (strcmp(arg[iarg],"file") == 0) fp = fopen(arg[iarg+1],"w"); else fp = fopen(arg[iarg+1],"a"); if (fp == NULL) - error->one(FLERR,fmt::format("Cannot open print file {}", - arg[iarg+1])); + error->one(FLERR,fmt::format("Cannot open print file {}: {}", + arg[iarg+1], utils::getsyserror())); } iarg += 2; } else if (strcmp(arg[iarg],"screen") == 0) { diff --git a/src/lammps.cpp b/src/lammps.cpp index 67c4d7aea6..7a6d1c9f3b 100644 --- a/src/lammps.cpp +++ b/src/lammps.cpp @@ -400,20 +400,25 @@ LAMMPS::LAMMPS(int narg, char **arg, MPI_Comm communicator) : else { universe->uscreen = fopen(arg[screenflag],"w"); if (universe->uscreen == NULL) - error->universe_one(FLERR,"Cannot open universe screen file"); + error->universe_one(FLERR,fmt::format("Cannot open universe screen " + "file {}: {}",arg[screenflag], + utils::getsyserror())); } if (logflag == 0) { if (helpflag == 0) { universe->ulogfile = fopen("log.lammps","w"); if (universe->ulogfile == NULL) - error->universe_warn(FLERR,"Cannot open log.lammps for writing"); + error->universe_warn(FLERR,"Cannot open log.lammps for writing: " + + utils::getsyserror()); } } else if (strcmp(arg[logflag],"none") == 0) universe->ulogfile = NULL; else { universe->ulogfile = fopen(arg[logflag],"w"); if (universe->ulogfile == NULL) - error->universe_one(FLERR,"Cannot open universe log file"); + error->universe_one(FLERR,fmt::format("Cannot open universe log " + "file {}: {}",arg[logflag], + utils::getsyserror())); } } @@ -437,8 +442,8 @@ LAMMPS::LAMMPS(int narg, char **arg, MPI_Comm communicator) : if (inflag == 0) infile = stdin; else infile = fopen(arg[inflag],"r"); if (infile == NULL) - error->one(FLERR,fmt::format("Cannot open input script {}", - arg[inflag])); + error->one(FLERR,fmt::format("Cannot open input script {}: {}", + arg[inflag], utils::getsyserror())); } if ((universe->me == 0) && !helpflag) @@ -461,45 +466,58 @@ LAMMPS::LAMMPS(int narg, char **arg, MPI_Comm communicator) : if (screenflag == 0) { str = fmt::format("screen.{}",universe->iworld); screen = fopen(str.c_str(),"w"); - if (screen == NULL) error->one(FLERR,"Cannot open screen file"); + if (screen == NULL) + error->one(FLERR,fmt::format("Cannot open screen file {}: {}", + str,utils::getsyserror())); } else if (strcmp(arg[screenflag],"none") == 0) { screen = NULL; } else { str = fmt::format("{}.{}",arg[screenflag],universe->iworld); screen = fopen(str.c_str(),"w"); - if (screen == NULL) error->one(FLERR,"Cannot open screen file"); + if (screen == NULL) + error->one(FLERR,fmt::format("Cannot open screen file {}: {}", + arg[screenflag],utils::getsyserror())); } } else if (strcmp(arg[partscreenflag],"none") == 0) { screen = NULL; } else { str = fmt::format("{}.{}",arg[partscreenflag],universe->iworld); screen = fopen(str.c_str(),"w"); - if (screen == NULL) error->one(FLERR,"Cannot open screen file"); + if (screen == NULL) + error->one(FLERR,fmt::format("Cannot open screen file {}: {}", + str,utils::getsyserror())); } if (partlogflag == 0) { if (logflag == 0) { str = fmt::format("log.lammps.{}",universe->iworld); logfile = fopen(str.c_str(),"w"); - if (logfile == NULL) error->one(FLERR,"Cannot open logfile"); + if (logfile == NULL) + error->one(FLERR,fmt::format("Cannot open logfile {}: {}", + str, utils::getsyserror())); } else if (strcmp(arg[logflag],"none") == 0) { logfile = NULL; } else { str = fmt::format("{}.{}",arg[logflag],universe->iworld); logfile = fopen(str.c_str(),"w"); - if (logfile == NULL) error->one(FLERR,"Cannot open logfile"); + if (logfile == NULL) + error->one(FLERR,fmt::format("Cannot open logfile {}: {}", + str, utils::getsyserror())); } } else if (strcmp(arg[partlogflag],"none") == 0) { logfile = NULL; } else { str = fmt::format("{}.{}",arg[partlogflag],universe->iworld); logfile = fopen(str.c_str(),"w"); - if (logfile == NULL) error->one(FLERR,"Cannot open logfile"); + if (logfile == NULL) + error->one(FLERR,fmt::format("Cannot open logfile {}: {}", + str, utils::getsyserror())); } infile = fopen(arg[inflag],"r"); if (infile == NULL) - error->one(FLERR,fmt::format("Cannot open input script {}",arg[inflag])); + error->one(FLERR,fmt::format("Cannot open input script {}: {}", + arg[inflag], utils::getsyserror())); } // screen and logfile messages for universe and world diff --git a/src/molecule.cpp b/src/molecule.cpp index 50ef344ea9..19d1ed23a2 100644 --- a/src/molecule.cpp +++ b/src/molecule.cpp @@ -1723,7 +1723,8 @@ void Molecule::open(char *file) { fp = fopen(file,"r"); if (fp == NULL) - error->one(FLERR,fmt::format("Cannot open molecule file {}",file)); + error->one(FLERR,fmt::format("Cannot open molecule file {}: {}", + file, utils::getsyserror())); } /* ---------------------------------------------------------------------- diff --git a/src/pair.cpp b/src/pair.cpp index 7d34e4fd3f..31a0ad6848 100644 --- a/src/pair.cpp +++ b/src/pair.cpp @@ -33,6 +33,8 @@ #include "memory.h" #include "math_const.h" #include "error.h" +#include "utils.h" +#include "fmt/format.h" using namespace LAMMPS_NS; using namespace MathConst; @@ -1639,7 +1641,9 @@ void Pair::write_file(int narg, char **arg) FILE *fp; if (me == 0) { fp = fopen(arg[6],"a"); - if (fp == NULL) error->one(FLERR,"Cannot open pair_write file"); + if (fp == NULL) + error->one(FLERR,fmt::format("Cannot open pair_write file {}: {}", + arg[6], utils::getsyserror())); fprintf(fp,"# Pair potential %s for atom types %d %d: i,r,energy,force\n", force->pair_style,itype,jtype); if (style == RLINEAR) diff --git a/src/potential_file_reader.cpp b/src/potential_file_reader.cpp index ea949ac984..cf90c588f5 100644 --- a/src/potential_file_reader.cpp +++ b/src/potential_file_reader.cpp @@ -38,8 +38,8 @@ PotentialFileReader::PotentialFileReader(LAMMPS *lmp, fp = force->open_potential(filename.c_str()); if (fp == NULL) - error->one(FLERR,fmt::format("Cannot open {} potential file {}", - potential_name, filename)); + error->one(FLERR,fmt::format("Cannot open {} potential file {}: {}", + potential_name, filename, utils::getsyserror())); } PotentialFileReader::~PotentialFileReader() { diff --git a/src/read_data.cpp b/src/read_data.cpp index edeb6398b8..80c25cf938 100644 --- a/src/read_data.cpp +++ b/src/read_data.cpp @@ -18,6 +18,7 @@ #include "read_data.h" #include #include +#include #include #include "atom.h" #include "atom_vec.h" @@ -42,6 +43,7 @@ #include "error.h" #include "memory.h" #include "utils.h" +#include "fmt/format.h" using namespace LAMMPS_NS; @@ -709,11 +711,8 @@ void ReadData::command(int narg, char **arg) if (firstpass) impropercoeffs(1); else skip_lines(nimpropertypes); - } else { - char str[128]; - snprintf(str,128,"Unknown identifier in data file: %s",keyword); - error->all(FLERR,str); - } + } else error->all(FLERR,fmt::format("Unknown identifier in data file: {}", + keyword)); parse_keyword(0); } @@ -2013,25 +2012,21 @@ void ReadData::open(char *file) if (!compressed) fp = fopen(file,"r"); else { #ifdef LAMMPS_GZIP - char gunzip[128]; - snprintf(gunzip,128,"gzip -c -d %s",file); - + std::string gunzip = fmt::format("gzip -c -d {}",file); #ifdef _WIN32 - fp = _popen(gunzip,"rb"); + fp = _popen(gunzip.c_str(),"rb"); #else - fp = popen(gunzip,"r"); + fp = popen(gunzip.c_str(),"r"); #endif #else - error->one(FLERR,"Cannot open gzipped file"); + error->one(FLERR,"Cannot open gzipped file: " + utils::getsyserror()); #endif } - if (fp == NULL) { - char str[128]; - snprintf(str,128,"Cannot open file %s",file); - error->one(FLERR,str); - } + if (fp == NULL) + error->one(FLERR,fmt::format("Cannot open file {}: {}", + file, utils::getsyserror())); } /* ---------------------------------------------------------------------- diff --git a/src/read_restart.cpp b/src/read_restart.cpp index 2bf34be0f6..ade751fdef 100644 --- a/src/read_restart.cpp +++ b/src/read_restart.cpp @@ -113,11 +113,9 @@ void ReadRestart::command(int narg, char **arg) *ptr = '%'; } else hfile = file; fp = fopen(hfile,"rb"); - if (fp == NULL) { - char str[128]; - snprintf(str,128,"Cannot open restart file %s",hfile); - error->one(FLERR,str); - } + if (fp == NULL) + error->one(FLERR,fmt::format("Cannot open restart file {}: {}", + hfile, utils::getsyserror())); if (multiproc) delete [] hfile; } @@ -282,12 +280,9 @@ void ReadRestart::command(int narg, char **arg) sprintf(procfile,"%s%d%s",file,iproc,ptr+1); *ptr = '%'; fp = fopen(procfile,"rb"); - if (fp == NULL) { - char str[128]; - snprintf(str,128,"Cannot open restart file %s",procfile); - error->one(FLERR,str); - } - + if (fp == NULL) + error->one(FLERR,fmt::format("Cannot open restart file {}: {}", + procfile, utils::getsyserror())); utils::sfread(FLERR,&flag,sizeof(int),1,fp,NULL,error); if (flag != PROCSPERFILE) error->one(FLERR,"Invalid flag in peratom section of restart file"); @@ -354,11 +349,9 @@ void ReadRestart::command(int narg, char **arg) sprintf(procfile,"%s%d%s",file,icluster,ptr+1); *ptr = '%'; fp = fopen(procfile,"rb"); - if (fp == NULL) { - char str[128]; - snprintf(str,128,"Cannot open restart file %s",procfile); - error->one(FLERR,str); - } + if (fp == NULL) + error->one(FLERR,fmt::format("Cannot open restart file {}: {}", + procfile, utils::getsyserror())); delete [] procfile; } @@ -718,12 +711,10 @@ void ReadRestart::header() } else if (flag == NPROCS) { nprocs_file = read_int(); - if (nprocs_file != comm->nprocs && me == 0) { - char msg[128]; - snprintf(msg,128,"Restart file used different # of processors: %d vs. %d", - nprocs_file,comm->nprocs); - error->warning(FLERR,msg); - } + if (nprocs_file != comm->nprocs && me == 0) + error->warning(FLERR,fmt::format("Restart file used different # of " + "processors: {} vs. {}",nprocs_file, + comm->nprocs)); // don't set procgrid, warn if different diff --git a/src/reader.cpp b/src/reader.cpp index ee032dc822..2a6e4bf90c 100644 --- a/src/reader.cpp +++ b/src/reader.cpp @@ -14,6 +14,8 @@ #include "reader.h" #include #include "error.h" +#include "utils.h" +#include "fmt/format.h" using namespace LAMMPS_NS; @@ -41,25 +43,21 @@ void Reader::open_file(const char *file) if (!compressed) fp = fopen(file,"r"); else { #ifdef LAMMPS_GZIP - char gunzip[1024]; - snprintf(gunzip,1024,"gzip -c -d %s",file); - + std::string gunzip = fmt::format("gzip -c -d {}",file); #ifdef _WIN32 - fp = _popen(gunzip,"rb"); + fp = _popen(gunzip.c_str(),"rb"); #else - fp = popen(gunzip,"r"); + fp = popen(gunzip.c_str(),"r"); #endif #else - error->one(FLERR,"Cannot open gzipped file"); + error->one(FLERR,"Cannot open gzipped file: " + utils::getsyserror()); #endif } - if (fp == NULL) { - char str[128]; - snprintf(str,128,"Cannot open file %s",file); - error->one(FLERR,str); - } + if (fp == NULL) + error->one(FLERR,fmt::format("Cannot open file {}: {}", + file, utils::getsyserror())); } /* ---------------------------------------------------------------------- diff --git a/src/universe.cpp b/src/universe.cpp index 036b3a7e66..0fad232d2c 100644 --- a/src/universe.cpp +++ b/src/universe.cpp @@ -20,6 +20,8 @@ #include "error.h" #include "force.h" #include "memory.h" +#include "utils.h" +#include "fmt/format.h" using namespace LAMMPS_NS; @@ -96,7 +98,10 @@ void Universe::reorder(char *style, char *arg) if (me == 0) { FILE *fp = fopen(arg,"r"); - if (fp == NULL) error->universe_one(FLERR,"Cannot open -reorder file"); + if (fp == NULL) + error->universe_one(FLERR,fmt::format("Cannot open -reorder " + "file {}: {}",arg, + utils::getsyserror())); // skip header = blank and comment lines @@ -117,7 +122,8 @@ void Universe::reorder(char *style, char *arg) rv = sscanf(line,"%d %d",&me_orig,&me_new); if (me_orig < 0 || me_orig >= nprocs || me_new < 0 || me_new >= nprocs || rv != 2) - error->one(FLERR,"Invalid entry in -reorder file"); + error->one(FLERR,fmt::format("Invalid entry '{} {}' in -reorder " + "file", me_orig, me_new)); uni2orig[me_new] = me_orig; for (int i = 1; i < nprocs; i++) { @@ -126,7 +132,8 @@ void Universe::reorder(char *style, char *arg) rv = sscanf(line,"%d %d",&me_orig,&me_new); if (me_orig < 0 || me_orig >= nprocs || me_new < 0 || me_new >= nprocs || rv != 2) - error->one(FLERR,"Invalid entry in -reorder file"); + error->one(FLERR,fmt::format("Invalid entry '{} {}' in -reorder " + "file", me_orig, me_new)); uni2orig[me_new] = me_orig; } fclose(fp); @@ -201,11 +208,9 @@ void Universe::add_world(char *str) if (n < 1 || nper < 1) valid = false; - if (!valid) { - char msg[128]; - snprintf(msg,128,"Invalid partition string '%s'",str); - error->universe_all(FLERR,msg); - } + if (!valid) + error->universe_all(FLERR,fmt::format("Invalid partition string '{}'", + str)); } else nper = nprocs; memory->grow(procs_per_world,nworlds+n,"universe:procs_per_world"); @@ -268,8 +273,7 @@ char *date2num(const char *version) year = atoi(version); } - char *ver = new char[64]; - sprintf(ver,"%04d%02d%02d", year % 10000, month, day % 100); - + char *ver = new char[12]; + snprintf(ver,12,"%04d%02d%02d", year % 10000, month, day % 100); return ver; } diff --git a/src/variable.cpp b/src/variable.cpp index fb6d2da8cc..7c9f7fc057 100644 --- a/src/variable.cpp +++ b/src/variable.cpp @@ -269,7 +269,8 @@ void Variable::set(int narg, char **arg) if (universe->me == 0) { FILE *fp = fopen("tmp.lammps.variable","w"); if (fp == NULL) - error->one(FLERR,"Cannot open temporary file for world counter."); + error->one(FLERR,"Cannot open temporary file for world counter: " + + utils::getsyserror()); fprintf(fp,"%d\n",universe->nworlds); fclose(fp); fp = NULL; @@ -532,12 +533,10 @@ void Variable::set(int narg, char **arg) strcpy(names[nvar],arg[0]); for (int i = 0; i < n-1; i++) - if (!isalnum(names[nvar][i]) && names[nvar][i] != '_') { - char errmsg[128]; - snprintf(errmsg,128,"Variable name '%s' must have only alphanumeric " - "characters or underscore",names[nvar]); - error->all(FLERR,errmsg); - } + if (!isalnum(names[nvar][i]) && names[nvar][i] != '_') + error->all(FLERR,fmt::format("Variable name '{}' must have only " + "alphanumeric characters or underscores", + names[nvar])); nvar++; } @@ -590,11 +589,9 @@ int Variable::next(int narg, char **arg) for (int iarg = 0; iarg < narg; iarg++) { ivar = find(arg[iarg]); - if (ivar < 0) { - char errmsg[128]; - snprintf(errmsg,128,"Invalid variable '%s' in next command",arg[iarg]); - error->all(FLERR,errmsg); - } + if (ivar < 0) + error->all(FLERR,fmt::format("Invalid variable '{}' in next command", + arg[iarg])); if (style[ivar] == ULOOP && style[find(arg[0])] == UNIVERSE) continue; else if (style[ivar] == UNIVERSE && style[find(arg[0])] == ULOOP) continue; else if (style[ivar] != style[find(arg[0])]) @@ -919,11 +916,9 @@ char *Variable::retrieve(char *name) str = data[ivar][1]; } else if (style[ivar] == PYTHON) { int ifunc = python->variable_match(data[ivar][0],name,0); - if (ifunc < 0) { - char errmsg[128]; - snprintf(errmsg,128,"Python variable '%s' does not match Python function",name); - error->all(FLERR,errmsg); - } + if (ifunc < 0) + error->all(FLERR,fmt::format("Python variable '{}' does not match " + "Python function", name)); python->invoke_function(ifunc,data[ivar][1]); str = data[ivar][1]; // if Python func returns a string longer than VALUELENGTH @@ -1638,12 +1633,9 @@ double Variable::evaluate(char *str, Tree **tree, int ivar) if (word[0] == 'F') lowercase = 0; int ifix = modify->find_fix(word+2); - if (ifix < 0) { - std::string mesg = "Invalid fix ID '"; - mesg += (word+2); - mesg += "' in variable formula"; - print_var_error(FLERR,mesg,ivar); - } + if (ifix < 0) + print_var_error(FLERR,fmt::format("Invalid fix ID '{}' in variable" + " formula",word+2),ivar); Fix *fix = modify->fix[ifix]; // parse zero or one or two trailing brackets @@ -2074,12 +2066,9 @@ double Variable::evaluate(char *str, Tree **tree, int ivar) argstack,nargstack,ivar)); else if (special_function(word,contents,tree,treestack,ntreestack, argstack,nargstack,ivar)); - else { - char msg[128]; - snprintf(msg,128,"Invalid math/group/special function '%s()'" - "in variable formula", word); - print_var_error(FLERR,msg,ivar); - } + else print_var_error(FLERR,fmt::format("Invalid math/group/special " + "function '{}()'in variable " + "formula", word),ivar); delete [] contents; // ---------------- @@ -2134,11 +2123,9 @@ double Variable::evaluate(char *str, Tree **tree, int ivar) "simulation box is defined",ivar); int flag = output->thermo->evaluate_keyword(word,&value1); - if (flag) { - char msg[128]; - snprintf(msg,128,"Invalid thermo keyword '%s' in variable formula",word); - print_var_error(FLERR,msg,ivar); - } + if (flag) + print_var_error(FLERR,fmt::format("Invalid thermo keyword '{}' in " + "variable formula",word),ivar); if (tree) { Tree *newtree = new Tree(); newtree->type = VALUE; @@ -5071,11 +5058,9 @@ VarReader::VarReader(LAMMPS *lmp, char *name, char *file, int flag) : if (me == 0) { fp = fopen(file,"r"); - if (fp == NULL) { - char str[128]; - snprintf(str,128,"Cannot open file variable file %s",file); - error->one(FLERR,str); - } + if (fp == NULL) + error->one(FLERR,fmt::format("Cannot open file variable file {}: {}", + file, utils::getsyserror())); } // if atomfile-style variable, must store per-atom values read from file diff --git a/src/write_coeff.cpp b/src/write_coeff.cpp index 8c2db1b7f4..612f5bb123 100644 --- a/src/write_coeff.cpp +++ b/src/write_coeff.cpp @@ -27,6 +27,7 @@ #include "error.h" #include "domain.h" #include "utils.h" +#include "fmt/format.h" using namespace LAMMPS_NS; @@ -56,10 +57,9 @@ void WriteCoeff::command(int narg, char **arg) char str[256], coeff[256]; FILE *one = fopen(file,"wb+"); - if (one == NULL) { - snprintf(str,256,"Cannot open coeff file %s",file); - error->one(FLERR,str); - } + if (one == NULL) + error->one(FLERR,fmt::format("Cannot open coeff file {}: {}", + file, utils::getsyserror())); if (force->pair && force->pair->writedata) { fprintf(one,"# pair_style %s\npair_coeff\n",force->pair_style); @@ -91,10 +91,10 @@ void WriteCoeff::command(int narg, char **arg) rewind(one); FILE *two = fopen(file+4,"w"); - if (two == NULL) { - snprintf(str,256,"Cannot open coeff file %s",file+4); - error->one(FLERR,str); - } + if (two == NULL) + error->one(FLERR,fmt::format("Cannot open coeff file {}: {}", + file+4, utils::getsyserror())); + fprintf(two,"# LAMMPS coeff file via write_coeff, version %s\n", universe->version); diff --git a/src/write_data.cpp b/src/write_data.cpp index 3d788b9665..6636d69fb2 100644 --- a/src/write_data.cpp +++ b/src/write_data.cpp @@ -32,6 +32,8 @@ #include "thermo.h" #include "memory.h" #include "error.h" +#include "utils.h" +#include "fmt/format.h" using namespace LAMMPS_NS; @@ -191,11 +193,9 @@ void WriteData::write(char *file) if (me == 0) { fp = fopen(file,"w"); - if (fp == NULL) { - char str[128]; - snprintf(str,128,"Cannot open data file %s",file); - error->one(FLERR,str); - } + if (fp == NULL) + error->one(FLERR,fmt::format("Cannot open data file {}: {}", + file, utils::getsyserror())); } // proc 0 writes header, ntype-length arrays, force fields diff --git a/src/write_restart.cpp b/src/write_restart.cpp index b1602332e0..accdf39583 100644 --- a/src/write_restart.cpp +++ b/src/write_restart.cpp @@ -35,6 +35,8 @@ #include "mpiio.h" #include "memory.h" #include "error.h" +#include "utils.h" +#include "fmt/format.h" #include "lmprestart.h" @@ -242,11 +244,9 @@ void WriteRestart::write(char *file) *ptr = '%'; } else hfile = file; fp = fopen(hfile,"wb"); - if (fp == NULL) { - char str[128]; - snprintf(str,128,"Cannot open restart file %s",hfile); - error->one(FLERR,str); - } + if (fp == NULL) + error->one(FLERR, fmt::format("Cannot open restart file {}: {}", + hfile, utils::getsyserror())); if (multiproc) delete [] hfile; } @@ -310,11 +310,9 @@ void WriteRestart::write(char *file) if (filewriter) { fp = fopen(multiname,"wb"); - if (fp == NULL) { - char str[128]; - snprintf(str,128,"Cannot open restart file %s",multiname); - error->one(FLERR,str); - } + if (fp == NULL) + error->one(FLERR, fmt::format("Cannot open restart file {}: {}", + multiname, utils::getsyserror())); write_int(PROCSPERFILE,nclusterprocs); }