38 lines
851 B
C
38 lines
851 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 fclose nötig, weil dies schon in der main passiert und mit *file übergeben wird//
|
|
|
|
|
|
if (file == NULL)
|
|
{
|
|
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;
|
|
}
|