From 7245877786f0ff4d90941ef91a87f74cd069aada Mon Sep 17 00:00:00 2001 From: Jens Burger Date: Sat, 25 Oct 2025 16:50:57 +0200 Subject: [PATCH] added readwords() --- Start_Windows/input.c | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/Start_Windows/input.c b/Start_Windows/input.c index 3f100f3..568400c 100644 --- a/Start_Windows/input.c +++ b/Start_Windows/input.c @@ -2,11 +2,37 @@ #include #include +#define MAX_BUFFER_LEN 256 + // TODO: // eine Funktion implementieren, die ein einzelnes Wort aus einer Textdatei (words.txt) einliest und als C-String zurückgibt. // Read words from file and store in 'words' array int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount) { + char buffer[MAX_BUFFER_LEN]; + int count = 0; + char *teiler = ".,; !?\n"; + if(file == NULL) + { + printf("Fehler beim Öffnen von words.txt!\n"); + return -1; + } + + while(fgets(buffer, MAX_LINE_LEN, file)) + { + const char *wort = strtok(buffer, teiler); + + while(wort != NULL && count < maxWordCount) + { + strncpy(words[count], wort, MAX_WORD_LEN-1); + words[count][MAX_WORD_LEN-1] = '\0'; + count++; + + wort = strtok(NULL, teiler); + } + } + + return count; }