diff --git a/Startcode/input.c b/Startcode/input.c index ed77805..97db7fb 100644 --- a/Startcode/input.c +++ b/Startcode/input.c @@ -5,8 +5,49 @@ // TODO: // eine Funktion implementieren, die ein einzelnes Wort aus einer Textdatei (words.txt) einliest und als C-String zurückgibt. +// Erkennen der Trennzeichen -> c = 1 wenn Trennzeichen +// Static ist interne Hilfsfunktion, die auserhalb nicht aufgerufen werden kann +static int istTrennzeichen (int c) +{ + return c == ' ' || c == ',' || c == ';' || c == '\n' || c == '\t'|| c == '\r'; +} + + +static int readWord(FILE *file, char *word) +{ + unsigned int len = 0; + int c = fgetc(file); + + // Trennzeichen vor Wort überspringen (EOF= End of File) + while(c != EOF && istTrennzeichen(c)) + { + c = fgetc(file); + } + + //Buchstaben sammeln bis Trennzeichen oder Dateiende + while(c != EOF && !istTrennzeichen(c)) + { + if(len < MAX_WORD_LEN -1) + { + word[len] = toupper(c); + len++; + } + c = fgetc(file); + } + + word[len] = '\0'; + return len > 0; +} + + // Read words from file and store in 'words' array int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount) { + unsigned int count = 0; + while(count < maxWordCount && readWord(file, words[count])) + { + count++; + } + return count; } \ No newline at end of file diff --git a/Startcode/main.c b/Startcode/main.c index 03da755..513e369 100644 --- a/Startcode/main.c +++ b/Startcode/main.c @@ -41,10 +41,19 @@ int main(int argc, char *argv[]) // Start the game if successful // error message if some words couldn't be placed + if(placedWords == wordCount) + { + //600 Pixel random gewählt, Keine daten dazu gefunden; + startGame(wordSalad, SALAD_SIZE, words, wordCount, 600); + } + else + { + fprintf(stderr, "Not all words could be placed ...\n"); + exitCode = EXIT_FAILURE; + } } else { - // Print error message if file couldn't be opened fprintf(stderr, "Could not open file %s for reading ...\n", argv[1]); exitCode = EXIT_FAILURE; }