#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. // Read words from file and store in 'words' array // int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount) // { // } // Wörter aus einer Datei lesen und im Array 'words' speichern // Gibt die Anzahl der gelesenen Wörter zurück oder -1 im Fehlerfall. int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount) { if (file == NULL) { fprintf(stderr, "Fehler: Datei ist NULL.\n"); return -1; } unsigned int count = 0; char line[MAX_WORD_LEN + 5]; // etwas Puffer, um Überlauf zu vermeiden // Zeilen einzeln lesen, bis EOF erreicht oder das Limit erreicht ist while (fgets(line, sizeof(line), file) != NULL && count < maxWordCount) { // Zeilenumbruch '\n' oder '\r\n' entfernen line[strcspn(line, "\r\n")] = '\0'; // Leere Zeilen überspringen if (strlen(line) == 0) continue; // Leerzeichen am Anfang und Ende entfernen char *start = line; while (isspace((unsigned char)*start)) start++; char *end = start + strlen(start) - 1; while (end > start && isspace((unsigned char)*end)) { *end = '\0'; end--; } // Sichere Kopie in das Array 'words' strncpy(words[count], start, MAX_WORD_LEN - 1); words[count][MAX_WORD_LEN - 1] = '\0'; // Nullterminierung sicherstellen count++; } return count; }