Einlesen der Wörter aus words.txt datei in input.c implementiert

This commit is contained in:
stoehrwe103998 committed 2026-10-28 21:37:17 +01:00
1 parent 40a1743c0b
commit aacde0c5f6
1 file changed
+62 -3
+62 -3
View File
@@ -4,9 +4,68 @@
// TODO: // TODO:
// eine Funktion implementieren, die ein einzelnes Wort aus einer Textdatei (words.txt) einliest und als C-String zurückgibt. // eine Funktion implementieren, die ein einzelnes Wort aus einer Textdatei (words.txt) einliest und als C-String zurückgibt.
static char *liesWort(FILE *file, char *puffer, unsigned int pufferGroesse) {
int laenge = 0;
if (file == NULL || puffer == NULL || pufferGroesse == 0) {
return NULL;
}
if (fgets(puffer, (int)pufferGroesse, file) == NULL) {
return NULL;
}
laenge = (int)strlen(puffer);
if (laenge > 0 && puffer[laenge - 1] == '\n') {
puffer[laenge - 1] = '\0';
laenge--;
}
if (laenge > 0 && puffer[laenge - 1] == '\r') {
puffer[laenge - 1] = '\0';
laenge--;
}
return puffer;
}
// Read words from file and store in 'words' array // Read words from file and store in 'words' array
int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount) int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount){
{
} int anzahlWoerter = 0;
int i = 0;
char zeilenPuffer[MAX_LINE_LEN];
char *wort = NULL;
const char *trennzeichen = " ,;\t\r\n";
if (file == NULL || words == NULL || maxWordCount == 0) {
return 0;
}
while (anzahlWoerter < (int)maxWordCount && liesWort(file, zeilenPuffer, sizeof(zeilenPuffer)) != NULL) {
wort = strtok(zeilenPuffer, trennzeichen);
while (wort != NULL && anzahlWoerter < (int)maxWordCount) {
while (*wort == ' ' || *wort == '\t') {
wort++;
}
if (*wort == '\0') {
wort = strtok(NULL, trennzeichen);
continue;
}
for (i = 0; wort[i] != '\0'; i++) {
wort[i] = (char)toupper((unsigned char)wort[i]);
}
strncpy(words[anzahlWoerter], wort, MAX_WORD_LEN - 1);
words[anzahlWoerter][MAX_WORD_LEN - 1] = '\0';
anzahlWoerter++;
wort = strtok(NULL, trennzeichen);
}
}
return anzahlWoerter;
}