commit a178d3897c34e30999974de6c5f37ab16d806669
parent 5ea06066e933f998baf70dc990f3fea7f82f8b20
Author: klewer-martin <martin.cachari@gmail.com>
Date: Tue, 9 Feb 2021 02:30:31 -0300
Update: Now the output has a variable length separator made of '-'
chars, the length depends on the digits of the number printed;
Diffstat:
7 files changed, 118 insertions(+), 22 deletions(-)
diff --git a/arguments.c b/arguments.c
@@ -53,3 +53,10 @@ status_t validate_arguments(int argc, char * argv[], char * src, char * dest)
return OK;
}
+bool file_are_the_same(char *file1, char *file2)
+{
+ if(strcmp(file1, file2))
+ return true;
+
+ return false;
+}
diff --git a/input.csv b/input.csv
@@ -2,7 +2,9 @@ PAIS, FECHA, INFECTADOS
32,1577880000,2342
32,1578657600,4923
32,1579089600,9324
-32,1579089600,9324
+32,1581223358,9324
+32,1581741758,7324
+32,1582864958,4324
170,1577880000,8234
170,1578657600,9234
170,1579089600,9423
@@ -10,4 +12,12 @@ PAIS, FECHA, INFECTADOS
276,1577880000,8432
276,1579089600,9129
276,1579521600,4214
-276,1579521600,4214
+276,1581223358,9324
+276,1581741758,7324
+276,1582864958,4324
+36,1577880000,3
+36,1579089600,7
+36,1579521600,16
+36,1581223358,33
+36,1581741758,88
+36,1582864958,162
diff --git a/main.c b/main.c
@@ -65,11 +65,15 @@ int main(int argc, char * argv[])
// Abre el archivo de entrada en modo lectura, y el de salida en modo
// escritura, si por algun motivo falla imprime un codigo de error;
- if((fpi = fopen(src, "r")) == NULL)
- return ERROR_READING_FILE;
+ if((fpi = fopen(src, "r")) == NULL) {
+ fclose(fpi);
+ return ERROR_READING_FILE;
+ }
- if((fpo = fopen(dest, "w")) == NULL)
- return ERROR_READING_FILE;
+ if((fpo = fopen(dest, "w")) == NULL) {
+ fclose(fpo);
+ return ERROR_READING_FILE;
+ }
size_t line;
for(line = 0; (st = read_file(fpi, &country, &date, &infected)) == OK; line++) {
@@ -79,15 +83,20 @@ int main(int argc, char * argv[])
st = OK;
}
- if(st != OK && st != END_OF_INPUT_FILE)
+ if(st != OK && st != END_OF_INPUT_FILE) {
+ close_files(fpi, fpo);
return st;
+ }
- fprintf(fpo, "Infectados por mes: %lu\n", infected_monthly);
- fprintf(fpo, "-------------------------\n\n");
-
- fclose(fpi);
- fclose(fpo);
+ fprintf_infected_monthly(fpo);
+ close_files(fpi, fpo);
printf(MSG_OK);
return OK;
}
+
+void close_files(FILE *fpi, FILE *fpo)
+{
+ fclose(fpi);
+ fclose(fpo);
+}
diff --git a/main.h b/main.h
@@ -5,6 +5,7 @@
#include <stdlib.h>
#include <string.h>
#include <time.h>
+#include <stdbool.h>
#include "macros.h"
@@ -45,5 +46,6 @@ typedef enum {
END_OF_INPUT_FILE,
} status_t;
+void close_files(FILE *fpi, FILE *fpo);
#endif
diff --git a/output.txt b/output.txt
@@ -10,13 +10,22 @@ Pais: Argentina
Fecha: 15 Jan 2020
Infectados: 9324
+Infectados por mes: 16589
+-------------------------
Pais: Argentina
-Fecha: 15 Jan 2020
+Fecha: 09 Feb 2020
Infectados: 9324
-Infectados por mes: 25913
--------------------------
+Pais: Argentina
+Fecha: 15 Feb 2020
+Infectados: 7324
+
+Pais: Argentina
+Fecha: 28 Feb 2020
+Infectados: 4324
+Infectados por mes: 20972
+-------------------------
Pais: Colombia
Fecha: 01 Jan 2020
Infectados: 8234
@@ -35,7 +44,6 @@ Infectados: 9423
Infectados por mes: 36314
-------------------------
-
Pais: Germany
Fecha: 01 Jan 2020
Infectados: 8432
@@ -48,10 +56,47 @@ Pais: Germany
Fecha: 20 Jan 2020
Infectados: 4214
+Infectados por mes: 21775
+-------------------------
Pais: Germany
-Fecha: 20 Jan 2020
-Infectados: 4214
+Fecha: 09 Feb 2020
+Infectados: 9324
-Infectados por mes: 25989
+Pais: Germany
+Fecha: 15 Feb 2020
+Infectados: 7324
+
+Pais: Germany
+Fecha: 28 Feb 2020
+Infectados: 4324
+
+Infectados por mes: 20972
-------------------------
+Pais: Australia
+Fecha: 01 Jan 2020
+Infectados: 3
+
+Pais: Australia
+Fecha: 15 Jan 2020
+Infectados: 7
+
+Pais: Australia
+Fecha: 20 Jan 2020
+Infectados: 16
+
+Infectados por mes: 26
+----------------------
+Pais: Australia
+Fecha: 09 Feb 2020
+Infectados: 33
+
+Pais: Australia
+Fecha: 15 Feb 2020
+Infectados: 88
+
+Pais: Australia
+Fecha: 28 Feb 2020
+Infectados: 162
+Infectados por mes: 283
+-----------------------
diff --git a/print_file.c b/print_file.c
@@ -21,12 +21,18 @@ status_t print_file(FILE *dest, char country_codes[COUNTRIES_NUMBER][ARRAYS_LENG
if((*(country) == prev_country) && (month == prev_month)) {
infected_monthly += *(infected);
}
- else if(*(country) != prev_country || month != prev_month) {
- fprintf(dest, "Infectados por mes: %lu\n", infected_monthly);
- fprintf(dest, "-------------------------\n\n");
+ else if(*(country) != prev_country) {
+ fprintf_infected_monthly(dest);
infected_monthly = *(infected);
prev_country = *(country);
+ prev_month = month;
+ }
+ else if(month != prev_month) {
+ prev_month = month;
+
+ fprintf_infected_monthly(dest);
+ infected_monthly = *(infected);
}
// Imprime datos segun el archivo de entrada;
@@ -92,4 +98,18 @@ status_t time_translator(time_t unix_time, char *res, size_t size, const char *f
return OK;
}
+void fprintf_infected_monthly(FILE *dest)
+{
+ char guion_medio[] = "-----------";
+
+ int length = snprintf( NULL, 0, "%lu", infected_monthly );
+ char* str_infected_monthly = (char *)malloc( length + 1 );
+ snprintf( str_infected_monthly, length + 1, "%lu", infected_monthly );
+
+ fprintf(dest, "Infectados por mes: %lu\n", infected_monthly);
+ sprintf(str_infected_monthly, "%lu", 10);
+ fprintf(dest, "-------------------%.*s\n", (length + 1), guion_medio);
+
+ free(str_infected_monthly);
+}
diff --git a/print_file.h b/print_file.h
@@ -10,6 +10,9 @@ status_t print_file(FILE *dest, char country_codes[COUNTRIES_NUMBER][ARRAYS_LENG
status_t fprintf_country(FILE *dest, size_t country_code, char country_codes[COUNTRIES_NUMBER][ARRAYS_LENGTH]);
status_t fprintf_date(FILE *dest, size_t date);
status_t fprintf_infected(FILE *dest, size_t infected, char newline);
+
+void fprintf_infected_monthly(FILE *dest);
+
status_t clean_buffer(char *buffer, size_t size);
status_t time_translator(time_t unix_time, char *res, size_t size, const char *format);