diff --git a/Start_Windows/input.c b/Start_Windows/input.c index 29b1aa4..32257e9 100644 --- a/Start_Windows/input.c +++ b/Start_Windows/input.c @@ -7,35 +7,39 @@ // Read words from file and store in 'words' array // f gets ; komma und semikolon erkennen und alles in groß buchstaben (=uppercase) (strtok) -int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount) +int readWords(FILE* file, char words[][MAX_WORD_LEN], unsigned int maxWordCount) { char delimiter[] = ", ; \n"; - char string[MAX_WORD_LEN]; - char stringcopy[MAX_WORD_LEN]; - char *stringptr; + unsigned int count = 0; + int shouldClose = 0; + - file = fopen("words.txt", "r"); if (file == NULL) { - printf("Fehler: Datei konnte nicht geöffnet werden!\n"); - return 1; + file = fopen("words.txt", "r"); + if (file == NULL) + { + printf("Fehler: Datei konnte nicht geöffnet werden!\n"); + return 1; + } + shouldClose = 1; } - else + while (fgets(words[count], MAX_WORD_LEN, file) != NULL && count < maxWordCount) { - printf("Datei konnte geöffnet werden"); - fgets(string, MAX_WORD_LEN, file); - strcpy(stringcopy, string); - stringptr = strtok(stringcopy, delimiter); - while (stringptr != NULL){ - stringptr = strtok(NULL, delimiter); - maxWordCount ++; - - } + char* token = strtok(words[count], delimiter); + while (token != NULL && count < maxWordCount) + { + for (char* p = token; *p; ++p) + (char)toupper((unsigned char)*p); + strncpy(words[count], token, MAX_WORD_LEN - 1); + words[count][MAX_WORD_LEN - 1] = '\0'; + count++; + token = strtok(NULL, delimiter); } } - - - + if (shouldClose)fclose(file); + return(int)count; +}