45 lines
1016 B
C
45 lines
1016 B
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;
|
|
|
|
if (file == NULL)
|
|
{
|
|
printf("\nNot able to open file.\n");
|
|
return -1;
|
|
}
|
|
|
|
int n = 0;
|
|
|
|
while (fgets(puffer, MAX_LINE_LEN, file) != NULL)
|
|
{
|
|
token = strtok(puffer, teiler);
|
|
|
|
while (token != NULL)
|
|
{
|
|
strncpy(words[n], token, MAX_WORD_LEN);
|
|
|
|
char *s = words[n];
|
|
while (*s)
|
|
{
|
|
*s = toupper((unsigned char)*s);
|
|
s++;
|
|
}
|
|
|
|
// printf("Einzelwort: %s\n", words[n]);
|
|
|
|
token = strtok(NULL, teiler);
|
|
n++;
|
|
}
|
|
}
|
|
return n;
|
|
} |