43 lines
1.3 KiB
C
43 lines
1.3 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 lines [(MAX_WORD_LEN + 2) * maxWordCount]; //Es wird davon ausgegangen, dass die Wörter nur durch eine Trennzeichen und ein Leerzeichen von einander gertrennt gespeichert werden
|
|
int word_counter = 0;
|
|
|
|
while (fgets(lines, sizeof(lines) , file) != NULL)
|
|
{
|
|
|
|
for (int i = 0; lines[i] != '\0'; i++) //Entfernen von \n aus dem String
|
|
{
|
|
lines[i] = toupper(lines[i]);
|
|
if (lines[i] == '\n')
|
|
{
|
|
lines[i] = '\0';
|
|
break;
|
|
}
|
|
}
|
|
|
|
char *single_word = strtok(lines, " ;,");
|
|
|
|
while (single_word != NULL && word_counter < maxWordCount)
|
|
{
|
|
|
|
strncpy(words[word_counter], single_word, MAX_WORD_LEN - 1);
|
|
words [word_counter][MAX_WORD_LEN -1] = '\0'; //Zur Sicherheit, damit \0 auf alle Fälle vorhanden ist
|
|
word_counter++;
|
|
single_word = strtok(NULL, " ;,");
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return word_counter;
|
|
} |