From 22c41a6cd05a984610a62ed21395e8f79c160ccc Mon Sep 17 00:00:00 2001 From: KRUGSON Date: Tue, 7 Feb 2023 15:20:15 +0100 Subject: [PATCH] refactoring: added func to print lines of stream --- src/c/main.c | 14 +------------- src/c/nav_helper.c | 16 ++++++++++++++++ src/c/nav_helper.h | 1 + 3 files changed, 18 insertions(+), 13 deletions(-) diff --git a/src/c/main.c b/src/c/main.c index 971f88e..5d72793 100644 --- a/src/c/main.c +++ b/src/c/main.c @@ -69,20 +69,8 @@ int main() void printInit() { FILE *stream; - char *line = NULL; - size_t len = 0; - ssize_t read; - stream = getStream(gameInstructionsFile); - - /* print line by line from file */ - while ((read = getline(&line, &len, stream)) != -1) - { - // printf("Retrieved line of length %u :\n", read); - printf("%s", line); - } - - free(line); /* Deallocate allocated memory */ + printStreamLines(stream); fclose(stream); /* closing file */ acceptInstructions(); diff --git a/src/c/nav_helper.c b/src/c/nav_helper.c index 78bdeb7..5a2d670 100644 --- a/src/c/nav_helper.c +++ b/src/c/nav_helper.c @@ -25,3 +25,19 @@ FILE *getStream(char *filePath) return stream; } + +void printStreamLines(FILE *stream){ + + char *line = NULL; + size_t len = 0; + ssize_t read; + + /* print line by line from file */ + while ((read = getline(&line, &len, stream)) != -1) + { + // printf("Retrieved line of length %u :\n", read); + printf("%s", line); + } + + free(line); /* Deallocate allocated memory */ +} diff --git a/src/c/nav_helper.h b/src/c/nav_helper.h index f90f1c2..056a4f1 100644 --- a/src/c/nav_helper.h +++ b/src/c/nav_helper.h @@ -8,5 +8,6 @@ bool startsWith(const char *a, const char *b); FILE *getStream(char *filePath); +void printStreamLines(FILE *stream); #endif \ No newline at end of file