From 25eed0fcaca4c72bfddb71ded3930912c8e672fb Mon Sep 17 00:00:00 2001 From: Thomas Schmucker Date: Sun, 27 Aug 2023 13:15:02 +0200 Subject: Einfacheres Speichermanagement implementiert --- csv-tutorial.c | 36 ++++++++++++------------------------ 1 file changed, 12 insertions(+), 24 deletions(-) (limited to 'csv-tutorial.c') diff --git a/csv-tutorial.c b/csv-tutorial.c index 17b95c8..9bdb7e6 100644 --- a/csv-tutorial.c +++ b/csv-tutorial.c @@ -8,9 +8,9 @@ bool processFile(const char *filename) { - FILE *in; + FILE *in = fopen(filename, "r"); - if ( (in = fopen(filename, "r")) == NULL ) { + if ( in == NULL ) { return false; } @@ -18,22 +18,21 @@ processFile(const char *filename) csv_init(&csv); while ( csv_read(&csv, in) ) { - const size_t nf = csv_nfields(&csv); - (void) nf; + const size_t nfields = csv_nfields(&csv); // ... } csv_cleanup(&csv); - fclose(in); + (void) fclose(in); return true; } bool processFile_options(const char *filename) { - FILE *in; + FILE *in = fopen(filename, "r"); - if ( (in = fopen(filename, "r")) == NULL ) { + if ( in == NULL ) { return false; } @@ -45,13 +44,12 @@ processFile_options(const char *filename) csv_init_opt(&csv, &csv_options); while ( csv_read(&csv, in) ) { - const size_t nf = csv_nfields(&csv); - (void) nf; + const size_t nfields = csv_nfields(&csv); // ... } csv_cleanup(&csv); - fclose(in); + (void) fclose(in); return true; } @@ -64,9 +62,9 @@ csvErrorHandler(csv_err_t err, void *cb_arg) bool processFile_error(const char *filename) { - FILE *in; + FILE *in = fopen(filename, "r"); - if ( (in = fopen(filename, "r")) == NULL ) { + if ( in == NULL ) { return false; } @@ -84,30 +82,20 @@ processFile_error(const char *filename) switch ( setjmp(env) ) { case CSV_ERR_OK: while ( csv_read(&csv, in) ) { - const size_t nf = csv_nfields(&csv); - (void) nf; + const size_t nfields = csv_nfields(&csv); // ... } break; case CSV_ERR_OUT_OF_MEMORY: - csv_cleanup(&csv); - break; - case CSV_ERR_OUT_OF_RANGE: - csv_cleanup(&csv); - break; - case CSV_ERR_IO_READ: - csv_cleanup(&csv); - break; - case CSV_ERR_IO_WRITE: csv_cleanup(&csv); break; } - fclose(in); + (void) fclose(in); return true; } -- cgit v1.3