diff --git a/.gitignore b/.gitignore index d5031e8..6fe2dc2 100644 --- a/.gitignore +++ b/.gitignore @@ -7,4 +7,7 @@ graphicalGame.o wordsalad wordsalad_initial.exe runTests.exe +testwords_delims.txt +testwords_empty.txt +testwords_simple.txt .vscode \ No newline at end of file diff --git a/Start_Linux/input.c b/Start_Linux/input.c index ed77805..ceab0b6 100644 --- a/Start_Linux/input.c +++ b/Start_Linux/input.c @@ -1,4 +1,5 @@ #include "input.h" +#include #include #include @@ -6,7 +7,39 @@ // 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) { + char buffer[MAX_WORD_LEN]; + unsigned int count = 0; -} \ No newline at end of file + if (file == NULL) + { + return 1; + } + + while (fgets(buffer, sizeof(buffer), file) != NULL) + { + // Zerlege Zeile in einzelne Wörter anhand der Trennzeichen + char *token = strtok(buffer, " ,;\n"); + while (token != NULL && count < maxWordCount) + { + // In Großbuchstaben umwandeln + for (int i = 0; token[i] != '\0'; i++) + { + token[i] = toupper((unsigned char)token[i]); + } + + // Token in das words-Array kopieren + strncpy(words[count], token, MAX_WORD_LEN - 1); + words[count][MAX_WORD_LEN - 1] = '\0'; // sicher terminieren + + count++; + + // Nächsten Token holen + token = strtok(NULL, " ,;\n"); + } + } + + return count; +} diff --git a/Start_Windows/input.c b/Start_Windows/input.c index ed77805..ceab0b6 100644 --- a/Start_Windows/input.c +++ b/Start_Windows/input.c @@ -1,4 +1,5 @@ #include "input.h" +#include #include #include @@ -6,7 +7,39 @@ // 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) { + char buffer[MAX_WORD_LEN]; + unsigned int count = 0; -} \ No newline at end of file + if (file == NULL) + { + return 1; + } + + while (fgets(buffer, sizeof(buffer), file) != NULL) + { + // Zerlege Zeile in einzelne Wörter anhand der Trennzeichen + char *token = strtok(buffer, " ,;\n"); + while (token != NULL && count < maxWordCount) + { + // In Großbuchstaben umwandeln + for (int i = 0; token[i] != '\0'; i++) + { + token[i] = toupper((unsigned char)token[i]); + } + + // Token in das words-Array kopieren + strncpy(words[count], token, MAX_WORD_LEN - 1); + words[count][MAX_WORD_LEN - 1] = '\0'; // sicher terminieren + + count++; + + // Nächsten Token holen + token = strtok(NULL, " ,;\n"); + } + } + + return count; +}