2025-10-26 16:59:21 +01:00

28 lines
878 B
C

#include "input.h"
#include <string.h>
#include <ctype.h>
// 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)
{
if (file == NULL) {
printf("Datei konnte nicht geoffnet werden");
return 0;
}
// 2D char Array um wörter zu speichern
char word[MAX_WORD_LEN];
unsigned int count = 0;
while (fscanf(file, "%s", word) != EOF && count < maxWordCount) {
// Kopiere das gelesene Wort in das 2D-Array
strncpy(words[count], word, MAX_WORD_LEN - 1);
words[count][MAX_WORD_LEN - 1] = '\0'; // Sicherstellen, dass der String nullterminiert ist
count++;
}
fclose(file);
return count;
}