input überarbeitet

This commit is contained in:
Björn 2025-10-26 16:59:21 +01:00
parent fbef3806db
commit 5162e9890d

View File

@ -8,21 +8,20 @@
// Read words from file and store in 'words' array
int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
{
//Erstellt Pointer auf textdokument
FILE *textdokument = fopen(file, "r");
if (textdokument == NULL) {
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(textdokument, "%s", word) != EOF && count < maxWordCount) {
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(textdokument);
fclose(file);
return count;