diff --git a/Startcode/input.c b/Startcode/input.c index ed77805..67f00a8 100644 --- a/Startcode/input.c +++ b/Startcode/input.c @@ -4,9 +4,68 @@ // TODO: // 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 -int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount) -{ +int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount){ -} \ No newline at end of file + 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; +}