#include "input.h" #include #include // 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]; int ch; int bufIndex = 0; // Alle Trennzeichen überspringen, bis ein echter Buchstabe kommt while ((ch = fgetc(file)) != EOF) { // Prüfen der Buchstaben if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z')) { // Wenn Kleinbuchstabe, in Großbuchstabe umwandeln if (ch >= 'a' && ch <= 'z') { ch = ch - 32; } buffer[bufIndex] = ch; bufIndex++; break; } } if (ch == EOF) { return NULL; } // Den Rest des Wortes einlesen (bricht bei Kommas, Leerzeichen etc. ab) while ((ch = fgetc(file)) != EOF && ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z'))) { if (bufIndex < 255) { if (ch >= 'a' && ch <= 'z') { ch = ch - 32; } buffer[bufIndex] = ch; bufIndex++; } } // Zwischenspeicher abschließen buffer[bufIndex] = '\0'; // Sicheres Kopieren in das Ziel-Array strncpy(destination, buffer, maxLen - 1); destination[maxLen - 1] = '\0'; return destination; } // 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; // Schleife läuft, solange noch Platz im Array ist while (count < maxWordCount) { if (readSingleWord(file, words[count], MAX_WORD_LEN) != NULL) { if (strlen(words[count]) > 0) // Prüfen, ob das Wort nicht leer ist { count++; // nächstes Wort } } else { break; // Bei Dateiende wird die Schleife abgebrochen } } return count; }