2025-10-24 13:59:35 +02:00

38 lines
843 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)
{
//kein fopen und close nötig, weil dies schon in der main passiert und mit *file übergeben wird//
if (!file)
{
perror("Error invalid pointer to file");
return -1;
}
char line[1024];
unsigned int count = 0;
while (fgets(line, sizeof(line), file) && count < maxWordCount)
{
line[strcspn(line, "\n")] = '\0';
char *token = strtok(line, " ;,");
while (token && count < maxWordCount)
{
strcpy(words[count], token);
count++;
token = strtok(NULL, " ;,");
}
}
return count;
}