48 lines
1.2 KiB
C
48 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 delimiter[] = ", ; \n";
|
|
unsigned int count = 0;
|
|
int shouldClose = 0;
|
|
|
|
|
|
if (file == NULL)
|
|
{
|
|
file = fopen("words.txt", "r");
|
|
if (file == NULL)
|
|
{
|
|
printf("Fehler: Datei konnte nicht geöffnet werden!\n");
|
|
return 1;
|
|
}
|
|
shouldClose = 1;
|
|
}
|
|
char line[MAX_LINE_LEN];
|
|
while (fgets(line, sizeof(line), file) != NULL && count < maxWordCount)
|
|
{
|
|
char *token = strtok(line, delimiter);
|
|
while (token != NULL && count < maxWordCount)
|
|
{
|
|
for (char *p = token; *p; ++p)
|
|
*p = (char)toupper((unsigned char)*p);
|
|
|
|
strncpy(words[count], token, MAX_WORD_LEN - 1);
|
|
words[count][MAX_WORD_LEN - 1] = '\0';
|
|
count++;
|
|
|
|
token = strtok(NULL, delimiter);
|
|
}
|
|
}
|
|
|
|
|
|
if (shouldClose)fclose(file);
|
|
return (int)count;
|
|
}
|