From c851f16d1df21d7579af72d842521b951261b092 Mon Sep 17 00:00:00 2001 From: Vincent Douillet Date: Thu, 2 Dec 2021 22:46:01 +0100 Subject: day 1 & 2 --- input.c | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 input.c (limited to 'input.c') diff --git a/input.c b/input.c new file mode 100644 index 0000000..5d184bb --- /dev/null +++ b/input.c @@ -0,0 +1,55 @@ +#include +#include +#include +#include + +/*public*/size_t count_lines(FILE* file) { + size_t lineCount = 0; + char currentChar; + // on part du debut + // TODO verifier le code de retour + fseek(file, 0, SEEK_SET); + while((currentChar=fgetc(file)) != EOF) { + if(currentChar == '\n') + lineCount++; + } + return lineCount; +} + +/*public*/int read_lines_as_int(FILE* file, int* result, int resultSize) { + // retour au debut du fichier et lecture des lignes + // TODO verifier le code de retour + fseek(file, 0, SEEK_SET); + int current; + for(int lineIndex = 0; lineIndex < resultSize; lineIndex++) { + if(fscanf(file, "%d", ¤t) != 1) { + result[lineIndex] = 0; + printf("Erreur de lecture ligne %d\n", lineIndex); + continue; + } + result[lineIndex] = current; + } + + return 0; +} + +/*public*/int read_lines_as_string(FILE* file, char** result, int resultSize) { + // retour au debut du fichier et lecture des lignes + // TODO verifier le code de retour + fseek(file, 0, SEEK_SET); + size_t lineSize = 0; + for(int lineIndex = 0; lineIndex < resultSize; lineIndex++) { + char** dst = &result[lineIndex]; + if(getline(dst, &lineSize, file) < 0) + err(1, "Erreur de lecture ligne %d\n", lineIndex); + } + + return 0; +} + +/*public*/void free_strings(char** strings, size_t stringCount) { + for(size_t i = 0; i < stringCount; i++) { + free(strings[i]); + } + free(strings); +} -- cgit v1.2.3