From 361e5ddacac3bba8d853e923890eed663853f91a Mon Sep 17 00:00:00 2001 From: Tabea Kleinlein Date: Fri, 30 Oct 2026 12:59:47 +0100 Subject: [PATCH] Hauptfunktion und Anpassung Hilfsfunktion in input.c --- Startcode/input.c | 35 ++++++++++++++++++++++++++--------- 1 file changed, 26 insertions(+), 9 deletions(-) diff --git a/Startcode/input.c b/Startcode/input.c index 991a12d..2f056b9 100644 --- a/Startcode/input.c +++ b/Startcode/input.c @@ -2,25 +2,42 @@ #include #include -// TODO: +// TODO: erledigt // eine Funktion implementieren, die ein einzelnes Wort aus einer Textdatei (words.txt) einliest und als C-String zurückgibt. char* readSingleWord(FILE *file, char *destination, int maxLen) { - char buffer[256]; // Zwischenspeicher zum einlesen - if (fgets(buffer, sizeof(buffer), file) != NULL) + char buffer[256]; + + // %255s schützt vor Pufferüberläufen des 256-Zeichen-buffers + if (fscanf(file, "%255s", buffer) == 1) { - buffer[strcspn(buffer, "\r\n")] = '\0'; // Entfernt Umbrüche - strncpy(destination, buffer, maxLen - 1); - destination[maxLen - 1] = '\0'; // Zwingendes Ende des C-Strings setzen, verhindert Pufferüberlauf + destination[maxLen - 1] = '\0'; // Zwingendes Ende des C-Strings setzen - return destination; //Rückgabe bei Erfolg + return destination; // Rückgabe bei Erfolg } - return NULL; //Rückgabe nur bei Dateiende + return NULL; // Rückgabe bei Dateiende } // Read words from file and store in 'words' array int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount) { - + unsigned int count = 0; + + while (count < maxWordCount) + { + // Aufruf Hilfsfunktion mit Arraygröße als Parameter + if (readSingleWord(file, words[count], MAX_WORD_LEN) != NULL) + { + if (strlen(words[count]) > 0) + { + count++; + } + } + else + { + break; // Ende der Datei erreicht + } + } + return count; } \ No newline at end of file