From 97ce35b2f0be82907da074e79308f065b06be2d3 Mon Sep 17 00:00:00 2001 From: Thomas Beutlich Date: Sun, 24 Mar 2024 17:42:21 +0100 Subject: [PATCH] Close file handle on exit --- src/readCSV.c | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/src/readCSV.c b/src/readCSV.c index 8608e108..548b1397 100644 --- a/src/readCSV.c +++ b/src/readCSV.c @@ -54,27 +54,29 @@ struct data readCSV(const char * filename, int skipLines) { FILE *fp; - if (file_exist(filename)) + if (!file_exist(filename)) { - fp = fopen(filename, "r"); - if (!(fp)) { - fprintf(stderr, "Cannot open file: %s\n", filename); - exit(1); - } - } else { fprintf(stderr, "No such file: %s\n", filename); exit(1); } + fp = fopen(filename, "r"); + if (!(fp)) { + fprintf(stderr, "Cannot open file: %s\n", filename); + exit(1); + } + time = malloc(sizeof(double) * arraySize); if (time == NULL){ fputs("Error: Failed to allocate memory for time.\n", stderr); + fclose(fp); exit(1); } value = malloc(sizeof(double) * arraySize); if (value == NULL){ - fputs("Error: Failed to allocate memory for value.\n", stderr); - exit(1); + fputs("Error: Failed to allocate memory for value.\n", stderr); + fclose(fp); + exit(1); } memset(time,0,sizeof(double)*arraySize); @@ -82,8 +84,9 @@ struct data readCSV(const char * filename, int skipLines) { for (i=0; i