From f2c07188198d6883e6f14e8c9d419155192478e3 Mon Sep 17 00:00:00 2001 From: Lew Rossman Date: Tue, 5 Mar 2019 16:15:33 -0500 Subject: [PATCH] Refactored closing/opening of output file --- src/epanet.c | 4 ++-- src/project.c | 10 +++++++++- src/report.c | 6 +++--- 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/src/epanet.c b/src/epanet.c index 383bd6f..57952ec 100644 --- a/src/epanet.c +++ b/src/epanet.c @@ -819,13 +819,13 @@ 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; } -*/ + return 0; } diff --git a/src/project.c b/src/project.c index b8e6119..0a40489 100644 --- a/src/project.c +++ b/src/project.c @@ -49,7 +49,11 @@ int openfiles(Project *pr, const char *f1, const char *f2, const char *f3) strncpy(pr->report.Rpt1Fname, f2, MAXFNAME); strncpy(pr->outfile.OutFname, f3, MAXFNAME); if (strlen(f3) > 0) pr->outfile.Outflag = SAVE; - else pr->outfile.Outflag = SCRATCH; + else + { + pr->outfile.Outflag = SCRATCH; + strcpy(pr->outfile.OutFname, pr->TmpOutFname); + } // Check that file names are not identical if (strlen(f1) > 0 && (strcomp(f1, f2) || strcomp(f1, f3))) return 301; @@ -181,6 +185,7 @@ int openoutfile(Project *pr) // If output file name was supplied, then attempt to // open it. Otherwise open a temporary output file. +/* if (pr->outfile.Outflag == SAVE) { pr->outfile.OutFile = fopen(pr->outfile.OutFname, "w+b"); @@ -192,6 +197,9 @@ int openoutfile(Project *pr) pr->outfile.OutFile = fopen(pr->outfile.OutFname, "w+b"); if (pr->outfile.OutFile == NULL) errcode = 304; } +*/ + pr->outfile.OutFile = fopen(pr->outfile.OutFname, "w+b"); + if (pr->outfile.OutFile == NULL) errcode = 304; // Save basic network data & energy usage results ERRCODE(savenetdata(pr)); diff --git a/src/report.c b/src/report.c index 4d0c4df..5605ffb 100644 --- a/src/report.c +++ b/src/report.c @@ -545,7 +545,7 @@ int writeresults(Project *pr) if (nnv == 0 && nlv == 0) return errcode; // Return if no output file -// if (outFile == NULL) outFile = fopen(pr->outfile.OutFname, "r+b"); + if (outFile == NULL) outFile = fopen(pr->outfile.OutFname, "rb"); if (outFile == NULL) return 106; // Allocate memory for output variables: @@ -587,14 +587,14 @@ int writeresults(Project *pr) time->Htime += time->Rstep; } } -/* + // Free output file if (outFile != NULL) { fclose(outFile); outFile = NULL; } -*/ + // Free allocated memory for (j = 0; j < m; j++) free(x[j]); free(x);