37 lines
1.3 KiB
C
37 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 puffer[MAX_LINE_LEN];
|
|
char *teiler = "\n ,;";
|
|
char *token;
|
|
unsigned int wordCount = 0;
|
|
|
|
while(fgets(puffer, MAX_LINE_LEN, file) != NULL) //Liest die zeilenweise Wörter aus der file in einen Zwischenpuffer bis das Dateiende erreicht ist returnwert der file == NULL
|
|
{
|
|
token = strtok(puffer, teiler); // Holt sich per Teilerbedingung das erste Wort aus dem puffer
|
|
|
|
while(token != NULL && wordCount < maxWordCount)
|
|
{
|
|
for(int i = 0; i < MAX_WORD_LEN; i++) //Verwandelt jeden Buchstaben in einen Großbuchstaben
|
|
{
|
|
token[i] = toupper(token[i]);
|
|
}
|
|
|
|
strncpy(words[wordCount], token, MAX_WORD_LEN -1); //speichert einzelnes Wort in das gegeben word array ab und holt sich das nächste im Puffer, bis array leer token == NULL
|
|
|
|
wordCount++;
|
|
|
|
token = strtok(NULL, teiler);
|
|
}
|
|
}
|
|
|
|
|
|
return wordCount;
|
|
} |