From 5a7f1d2a59adb62e530ffd67b746e176463561f3 Mon Sep 17 00:00:00 2001 From: Peter Wiebe Date: Tue, 6 Feb 2024 18:28:30 +0100 Subject: [PATCH] Commit 20 - func lade secret_word zum laden des wortes soll erstellt werden --- src/main/c/hangman.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/main/c/hangman.c b/src/main/c/hangman.c index 4df1645..0c3bb8d 100644 --- a/src/main/c/hangman.c +++ b/src/main/c/hangman.c @@ -16,6 +16,7 @@ void hauptSpiel(int level); void draw_hangman(int attempts); void add_guessed_letter(char* guessed_letters, char guessed_letter); char eingabe_buchstabe(); +void lade_secret_word(char* secret_word, int level, char* path); void protokoll(){ clear_screen(); @@ -267,4 +268,27 @@ char eingabe_buchstabe(){ scanf(" %c", &guess); guess = tolower(guess); return guess; +} + +void lade_secret_word(char* secret_word, int level, char* path){ + FILE *file = fopen(path, "r"); + int count = 0; + if ( file != NULL ) + { + char line[100]; /* or other suitable maximum line size */ + while (fgets(line, sizeof line, file) != NULL) /* read a line */ + { + if (count == level) + { + strcpy(secret_word, line); + secret_word[strlen(secret_word) - 1] = '\0'; + return; + } + else + { + count++; + } + } + fclose(file); + } } \ No newline at end of file