From def3034ede55da31a10fe981aa7d8db73469cccc Mon Sep 17 00:00:00 2001 From: Tabea Kleinlein Date: Tue, 27 Oct 2026 12:46:37 +0100 Subject: [PATCH] Hilfsfunktion in input.c --- Startcode/input.c | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/Startcode/input.c b/Startcode/input.c index ed77805..991a12d 100644 --- a/Startcode/input.c +++ b/Startcode/input.c @@ -2,8 +2,22 @@ #include #include -// TODO: +// TODO: // 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) + { + 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 + + return destination; //Rückgabe bei Erfolg + } + return NULL; //Rückgabe nur bei Dateiende +} // Read words from file and store in 'words' array int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)