forked from freudenreichan/info2Praktikum-Wortsalat
33 lines
985 B
C
33 lines
985 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)
|
|
{
|
|
if (file == NULL) {
|
|
return 0;
|
|
}
|
|
char line[MAX_LINE_LEN];
|
|
unsigned int count = 0;
|
|
|
|
while (fgets(line, sizeof(line), file) != NULL && count < maxWordCount) {
|
|
char *token = strtok(line, " ,;\n\r\t");
|
|
while (token != NULL & count < maxWordCount) {
|
|
if (strlen(token) > 0) {
|
|
strncpy(words[count], token, MAX_WORD_LEN - 1);
|
|
words[count][MAX_WORD_LEN - 1] = '\0';
|
|
for (int i= 0; words[count][i] != '\0'; i++) {
|
|
words[count][i] = toupper((unsigned char)words[count][i]);
|
|
}
|
|
count++;
|
|
}
|
|
token = strtok(NULL, " ,;\n\r\t");
|
|
}
|
|
}
|
|
return count;
|
|
|
|
} |