#include "input.h" #include #include // TODO: // eine Funktion implementieren, die ein einzelnes Wort aus einer Textdatei (words.txt) einliest und als C-String zurückgibt. string readWord(FILE *file) { char word[MAX_WORD_LEN]; // Puffer für Wort int index = 0; // Position im Puffer int c; // Variablefür jedes gelesene Zeichen // Whitespace überspringen while(isspace(c = fgetc(file))); //isspace checkt ob gelesenes Zeichen whitespace ist, wenn ja 1 wenn nein 0 // Buchstaben einlesen bis nächstes whitespace/EOF while(c != EOF && !isspace(c) && index < MAX_WORD_LEN - 1) // -1 wegen Nullterminator { word[index++] = (char)c; c = fgetc(file); } word[index] = '\0'; // Nullterminator (= Ende String) return strdup(word); // Rückgabe string, dynamisch allokiert da Zeiger auf lokalen Puffer zurückgegeben } // 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 && !feof(file)) { char *word = readWord(file); if(word != NULL) { strncpy(words[count], word, MAX_WORD_LEN - 1); // Wort in Array kopieren words[count][MAX_WORD_LEN - 1] = '\0'; // Sicherstellen, dass String nullterminiert ist free(word); // Dynamisch allokierten Speicher freigeben count++; } } return count; // Anzahl der gelesenen Wörter zurückgeben }