From bda3c2b1047a3a2c7759095f80e615f9050072a7 Mon Sep 17 00:00:00 2001 From: kachelto100370 Date: Tue, 4 Nov 2025 02:52:17 +0100 Subject: [PATCH] =?UTF-8?q?kristins=20input.c=20datei=20f=C3=BCr=20mich=20?= =?UTF-8?q?leserlicher=20gemacht?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Start_Linux/input.c | 38 ++++++++++++++++++++++++++++++++------ 1 file changed, 32 insertions(+), 6 deletions(-) diff --git a/Start_Linux/input.c b/Start_Linux/input.c index ed77805..b68814d 100644 --- a/Start_Linux/input.c +++ b/Start_Linux/input.c @@ -1,12 +1,38 @@ #include "input.h" -#include #include - +#include + // TODO: -// eine Funktion implementieren, die ein einzelnes Wort aus einer Textdatei (words.txt) einliest und als C-String zurückgibt. - +// 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) +int readWords(FILE *file, char words[][MAX_WORD_LEN],unsigned int maxWordCount) { - + + if (file == NULL) + { + perror("File couldn't be opened"); + } + + char fehlerhafterString[MAX_LINE_LEN]; + char *teiler = " ;,.\n"; + char *aktuellesWort; + int wortAnzahl = 0; + + while (fgets(fehlerhafterString, sizeof(fehlerhafterString), file) != NULL && wortAnzahl < maxWordCount) + { + + aktuellesWort = strtok(fehlerhafterString, teiler); + + while (aktuellesWort != NULL && wortAnzahl < maxWordCount) + { + strncpy(words[wortAnzahl], aktuellesWort,sizeof(words[wortAnzahl]) - 1); + words[wortAnzahl][sizeof(words[wortAnzahl]) - 1] = '\0'; + wortAnzahl++; // Nächstes Wort + aktuellesWort = strtok(NULL, teiler); // Nächstes Token + } + } + + return wortAnzahl; // Anzahl der eingelesenen Wörter } \ No newline at end of file