2025-11-04 10:48:06 +01:00

46 lines
1.2 KiB
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)
{
char delimiter[] = ", ; \n";
unsigned int count = 0;
int shouldClose = 0;
if (file == NULL)
{
file = fopen("words.txt", "r");
if (file == NULL)
{
printf("Fehler: Datei konnte nicht geöffnet werden!\n");
return 1;
}
shouldClose = 1;
}
while (fgets(words[count], MAX_WORD_LEN, file) != NULL && count < maxWordCount)
{
char* token = strtok(words[count], delimiter);
while (token != NULL && count < maxWordCount)
{
for (char* p = token; *p; ++p)
(char)toupper((unsigned char)*p);
strncpy(words[count], token, MAX_WORD_LEN - 1);
words[count][MAX_WORD_LEN - 1] = '\0';
count++;
token = strtok(NULL, delimiter);
}
}
if (shouldClose)fclose(file);
return(int)count;
}