generated from freudenreichan/info2Praktikum-Wortsalat
58 lines
1.4 KiB
C
58 lines
1.4 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)
|
|
{
|
|
int c;
|
|
unsigned int i = 0; // Wort
|
|
unsigned int j = 0; // Zeichen im Wort
|
|
|
|
while ((c = fgetc(file)) != EOF)
|
|
{
|
|
if (c == ',' || c == ';' || c == '\n' || c == ' ')
|
|
{
|
|
if (j > 0)
|
|
{
|
|
words[i][j] = '\0';
|
|
i++;
|
|
j = 0;
|
|
|
|
if (i >= maxWordCount)
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
// Auf Pufferüberlauf im Wort achten
|
|
if (j < MAX_WORD_LEN - 1)
|
|
{
|
|
words[i][j] = (char)toupper(c);
|
|
j++;
|
|
}
|
|
}
|
|
}
|
|
|
|
// Ans Ende \0 hinzufügen
|
|
if (j > 0 && i < maxWordCount)
|
|
{
|
|
words[i][j] = '\0';
|
|
i++;
|
|
}
|
|
|
|
// MIT CHAT GEMACHT ZUR KONTROLLE: Konsolenausgabe zur Kontrolle der eingelesenen Wörter
|
|
printf("Eingelesene Wörter (%u Stück):\n", i);
|
|
for (unsigned int idx = 0; idx < i; idx++)
|
|
{
|
|
printf("%u. Entry: %s\n", idx + 1, words[idx]);
|
|
}
|
|
|
|
|
|
return i;
|
|
} |