#include "input.h" #include #include // TODO: // eine Funktion implementieren, die ein einzelnes Wort aus einer Textdatei (words.txt) einliest und als C-String zurückgibt. // HIlfsfunktion: LIest das nächste Wort(Tocken) aus der Datei und gibt es als C-String züruck char* readNextWord(FILE *file, char *lineBuffer, unsigned int maxLineLen, int *isNewLineNeeded){ const char *delimiters = " ,;\t\r\n"; char *token = NULL; //Falls wir schon in einer Zeile sind, Versuche das nächste Word daraus zu holen if(!(*isNewLineNeeded)){ token = strtok(NULL, delimiters); } while (token == NULL) { if(fgets(lineBuffer, maxLineLen, file) == NULL) { // Dateiend (EOF) erreicht return NULL; } token = strtok(lineBuffer, delimiters); *isNewLineNeeded = 0; } return token; } // Read words from file and store in 'words' array int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount) { if(file == NULL || words == NULL || maxWordCount == 0){ return 0; } char lineBuffer[MAX_LINE_LEN]; int isNewLineNeeded = 1; unsigned int count = 0; char *word = NULL; while(count < maxWordCount && (word = readNextWord(file, lineBuffer, MAX_LINE_LEN, &isNewLineNeeded)) != NULL) { //Sicher kopieren mit strncpy und Nulltermination garantieren strncpy(words[count], word, MAX_WORD_LEN - 1); words[count][MAX_WORD_LEN - 1] = '\0'; //changing all leter to caplogs for(unsigned int i = 0; words[count][i] != '\0'; i++){ words[count][i] = (char)toupper((unsigned char)words[count][i]); } count++; } return (int)count; }