Files
Wortsalalat/input.c
T

35 lines
1.1 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)
{
const char *delim = ", ; \n\t";
char buffer[MAX_WORD_LEN];
int i = 0; // Wort-Zähler
// Prüfen, ob das Lesen erfolgreich war (EOF oder Fehler abfangen)
while (fgets(buffer, sizeof(buffer), file) != NULL && i<maxWordCount) {
char *wordbuffer = strtok(buffer, delim);
while (wordbuffer != NULL && i<maxWordCount) {
// snprintf ist quasi strncpy nur mit \0 und sicher
snprintf(words[i], MAX_WORD_LEN, "%s", wordbuffer);
for (int j=0; words[i][j] != '\0'; j++) { // Alles auf Großbuchstaben
words[i][j] = toupper((unsigned char)words[i][j]);
}
wordbuffer = strtok(NULL, delim);
i++;
}
}
return i;
}