36 lines
1.0 KiB
C
36 lines
1.0 KiB
C
#include "input.h"
|
|
#include <ctype.h>
|
|
#include <string.h>
|
|
|
|
// eine Funktion implementieren, die ein einzelnes Wort aus einer Textdatei
|
|
// (words.txt) einliest und als C-String zurückgibt.
|
|
|
|
int readWords(FILE *file, char words[][MAX_WORD_LEN],unsigned int maxWordCount)
|
|
{
|
|
char puffer[MAX_WORD_LEN];
|
|
file = fopen("words.txt", "r");
|
|
if (file == NULL)
|
|
{
|
|
return -1;
|
|
}
|
|
|
|
char teiler[] = " ;,.\n";
|
|
char *aktuellesWort;
|
|
int wortAnzahl = 0;
|
|
|
|
while (fgets(puffer, MAX_WORD_LEN, file) != NULL)
|
|
{
|
|
|
|
aktuellesWort = strtok(puffer, teiler);
|
|
|
|
while (aktuellesWort != NULL && wortAnzahl < maxWordCount)
|
|
{
|
|
strncpy(words[wortAnzahl], aktuellesWort,sizeof(words[wortAnzahl]) - 1);
|
|
words[wortAnzahl][sizeof(words[wortAnzahl]) - 1] = '\0';
|
|
wortAnzahl++; // Nächstes Wort
|
|
aktuellesWort = strtok(NULL, teiler); // Nächstes Token
|
|
}
|
|
}
|
|
fclose(file);
|
|
return wortAnzahl; // Anzahl der eingelesenen Wörter
|
|
} |