40 lines
1.2 KiB
C
40 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)
|
|
{
|
|
|
|
char puffer[256];
|
|
char *teiler = " ,;.\n";
|
|
char *token;
|
|
unsigned int wordCount = 0;
|
|
|
|
/* wird words.txt schon in main geöffnet?
|
|
file = fopen ("words.txt", "r"); // Öffne words.txt zum lesen
|
|
|
|
if (file == NULL)
|
|
{
|
|
return -1;
|
|
}
|
|
*/
|
|
|
|
while (fgets (puffer, 256, file) != NULL) //liest words.txt ein und speichert es in puffer
|
|
{
|
|
token = strtok(puffer, teiler); //token teilt erstes Wort und speichert es in sich ab
|
|
|
|
while (token != NULL && wordCount < maxWordCount)
|
|
{
|
|
strncpy(words[wordCount], token, MAX_WORD_LEN -1); //kopiert das Wort von token in das array words
|
|
words[wordCount][MAX_WORD_LEN-1] = '\0';
|
|
wordCount++;
|
|
token = strtok (NULL, teiler); //nächstes Wort
|
|
}
|
|
}
|
|
|
|
return wordCount; //Anzahl der gelesenen Wörter
|
|
} |