for consistent behavior we must not close the file pointer when it was passed as argument

This commit is contained in:
Axel Kohlmeyer
2021-04-26 12:12:19 -04:00
parent 8af1530e29
commit dbd7d454b9
2 changed files with 4 additions and 3 deletions

View File

@ -41,7 +41,7 @@ using namespace LAMMPS_NS;
* \param filetype Description of file type for error messages */ * \param filetype Description of file type for error messages */
TextFileReader::TextFileReader(const std::string &filename, const std::string &filetype) TextFileReader::TextFileReader(const std::string &filename, const std::string &filetype)
: filetype(filetype), ignore_comments(true) : filetype(filetype), closefp(true), ignore_comments(true)
{ {
fp = fopen(filename.c_str(), "r"); fp = fopen(filename.c_str(), "r");
@ -58,7 +58,7 @@ TextFileReader::TextFileReader(const std::string &filename, const std::string &f
* \param filetype Description of file type for error messages */ * \param filetype Description of file type for error messages */
TextFileReader::TextFileReader(FILE *fp, const std::string &filetype) TextFileReader::TextFileReader(FILE *fp, const std::string &filetype)
: filetype(filetype), fp(fp), ignore_comments(true) : filetype(filetype), closefp(false), fp(fp), ignore_comments(true)
{ {
if (fp == nullptr) throw FileReaderException("Invalid file descriptor"); if (fp == nullptr) throw FileReaderException("Invalid file descriptor");
} }
@ -66,7 +66,7 @@ TextFileReader::TextFileReader(FILE *fp, const std::string &filetype)
/** Closes the file */ /** Closes the file */
TextFileReader::~TextFileReader() { TextFileReader::~TextFileReader() {
fclose(fp); if (closefp) fclose(fp);
} }
/** Read the next line and ignore it */ /** Read the next line and ignore it */

View File

@ -26,6 +26,7 @@ namespace LAMMPS_NS
{ {
class TextFileReader { class TextFileReader {
std::string filetype; std::string filetype;
bool closefp;
static constexpr int MAXLINE = 1024; static constexpr int MAXLINE = 1024;
char line[MAXLINE]; char line[MAXLINE];
FILE *fp; FILE *fp;