diff --git a/src/epanet.c b/src/epanet.c index d8ba333..e39c752 100644 --- a/src/epanet.c +++ b/src/epanet.c @@ -819,13 +819,7 @@ int DLLEXPORT EN_closeQ(EN_Project p) if (!p->Openflag) return 102; closequal(p); p->quality.OpenQflag = FALSE; - - if (p->outfile.OutFile != NULL) - { - fclose(p->outfile.OutFile); - p->outfile.OutFile = NULL; - } - + closeoutfile(p); return 0; } diff --git a/src/funcs.h b/src/funcs.h index dde13c4..154dcfc 100755 --- a/src/funcs.h +++ b/src/funcs.h @@ -24,6 +24,7 @@ void freedata(Project *); int openfiles(Project *, const char *, const char *,const char *); int openhydfile(Project *); int openoutfile(Project *); +void closeoutfile(Project *); int buildadjlists(Network *); void freeadjlists(Network *); diff --git a/src/project.c b/src/project.c index 4d1621f..aceffa8 100644 --- a/src/project.c +++ b/src/project.c @@ -173,18 +173,8 @@ int openoutfile(Project *pr) int errcode = 0; // Close output file if already opened - if (pr->outfile.TmpOutFile != pr->outfile.OutFile && - pr->outfile.TmpOutFile != NULL) - { - fclose(pr->outfile.TmpOutFile); - pr->outfile.TmpOutFile = NULL; - } - if (pr->outfile.OutFile != NULL) - { - fclose(pr->outfile.OutFile); - pr->outfile.OutFile = NULL; - } - + closeoutfile(pr); + // If output file name was supplied, then attempt to // open it. Otherwise open a temporary output file. pr->outfile.OutFile = fopen(pr->outfile.OutFname, "w+b"); @@ -209,6 +199,33 @@ int openoutfile(Project *pr) return errcode; } +void closeoutfile(Project *pr) +/*---------------------------------------------------------------- +** Input: none +** Output: none +** Purpose: closes binary output file. +**---------------------------------------------------------------- +*/ +{ + if (pr->outfile.TmpOutFile != pr->outfile.OutFile) + { + if (pr->outfile.TmpOutFile != NULL) + { + fclose(pr->outfile.TmpOutFile); + pr->outfile.TmpOutFile = NULL; + } + } + if (pr->outfile.OutFile != NULL) + { + if (pr->outfile.OutFile == pr->outfile.TmpOutFile) + { + pr->outfile.TmpOutFile = NULL; + } + fclose(pr->outfile.OutFile); + pr->outfile.OutFile = NULL; + } +} + void initpointers(Project *pr) /*---------------------------------------------------------------- ** Input: none