kristins input.c datei für mich leserlicher gemacht

This commit is contained in:
Tobias Kachel 2025-11-04 02:52:17 +01:00
parent 65b20f7ea4
commit bda3c2b104

View File

@ -1,12 +1,38 @@
#include "input.h"
#include <string.h>
#include <ctype.h>
#include <string.h>
// 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)
{
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
}