46 lines
1.2 KiB
C
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)
|
|
{
|
|
unsigned int count = 0;
|
|
|
|
char line[MAX_LINE_LEN];
|
|
char *teiler = " ,;\n\t";
|
|
char *token;
|
|
|
|
while(fgets(line, sizeof(line), file)){
|
|
|
|
//Yeti,Nessie Werwolf; Vampir
|
|
// token = Yeti
|
|
token = strtok(line, teiler);
|
|
|
|
while (token != NULL)
|
|
{
|
|
if (strlen(token) >= MAX_WORD_LEN) {
|
|
printf("Fehler: eigelesenes Wort ist zu lang\n");
|
|
return count;
|
|
}
|
|
if (count >= maxWordCount){
|
|
printf("Fehler: zu viele Wörter\n");
|
|
return count;
|
|
}
|
|
|
|
strncpy(words[count], token, MAX_WORD_LEN-1);
|
|
words[count][MAX_WORD_LEN-1] = '\0';
|
|
|
|
//Nessie Werwolf; Vampir
|
|
//token= Nessie
|
|
token = strtok(NULL, teiler);
|
|
|
|
count++;
|
|
}
|
|
}
|
|
|
|
return count;
|
|
} |