forked from freudenreichan/info2Praktikum-Wortsalat
32 lines
829 B
C
32 lines
829 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)
|
|
{
|
|
char line[1024];
|
|
unsigned int count = 0;
|
|
|
|
if (file == NULL)
|
|
return 0;
|
|
|
|
while (count < maxWordCount && fgets(line, sizeof(line), file) != NULL)
|
|
{
|
|
char *word = strtok(line, ",; \t\r\n");
|
|
|
|
while (word != NULL && count < maxWordCount)
|
|
{
|
|
strncpy(words[count], word, MAX_WORD_LEN - 1);
|
|
words[count][MAX_WORD_LEN - 1] = '\0';
|
|
|
|
count++;
|
|
word = strtok(NULL, ",; \t\r\n");
|
|
}
|
|
}
|
|
|
|
return count;
|
|
} |