diff options
author | Vincent Douillet <vincent@vdouillet.fr> | 2021-12-02 22:46:01 +0100 |
---|---|---|
committer | Vincent Douillet <vincent@vdouillet.fr> | 2021-12-02 22:46:01 +0100 |
commit | c851f16d1df21d7579af72d842521b951261b092 (patch) | |
tree | 2f1c4672b53afb8c575ff7c64d88b9020034219d /input.c |
day 1 & 2
Diffstat (limited to 'input.c')
-rw-r--r-- | input.c | 55 |
1 files changed, 55 insertions, 0 deletions
@@ -0,0 +1,55 @@ +#include <stdlib.h> +#include <stdio.h> +#include <string.h> +#include <err.h> + +/*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); +} |