From f036995e78f6f95756d71400a2aa571294a9bba0 Mon Sep 17 00:00:00 2001 From: Lukas Weber Date: Tue, 4 Nov 2025 19:54:04 +0100 Subject: [PATCH] Finished input.c V1, starting test and fix phase --- Start_Windows/input.c | 45 ++++++++++++++++++++++++++----------------- 1 file changed, 27 insertions(+), 18 deletions(-) diff --git a/Start_Windows/input.c b/Start_Windows/input.c index 4922fd9..94b1846 100644 --- a/Start_Windows/input.c +++ b/Start_Windows/input.c @@ -1,30 +1,39 @@ #include "input.h" #include #include +void readWordsFromLine(char buffer[], int* wordCount,char words[][MAX_WORD_LEN], unsigned int maxWords) { + char *token; + char *teiler = " ,;"; + token = strtok(buffer, teiler); + + while (*wordCount < maxWords && token != NULL) { //condition for reading new words from buffer + if (strlen(token) <= MAX_WORD_LEN) { //checks if word fits into buffer + for (int i = 0; i < strlen(token); i++) { + if (token[i] == '\n') + break; + words[*wordCount][i] = token[i]; + } + (*wordCount)++; + } + printf("%s\n", token); + token = strtok(NULL, teiler); + } + +} + + // TODO: // 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[101]; // prepares a large enough buffer to read one line from the .txt-file - char *wordseperator = " ,;"; // sets up checks for seperated words - char *token; - int readwords = 0; - - while (readwords < maxWordCount) { - - do { - token = strtok(buffer, wordseperator); - } while (*token == ' ' || *token == ',' || *token == ';'); - if (strlen(token) <= MAX_WORD_LEN) { - for (int i = 0; i < strlen(token); i++) { - words[readwords][i] = token[i]; - } - readwords++; - } - token = strtok(NULL, wordseperator); + char buffer[MAX_LINE_LEN]; // prepares a large enough buffer to read one line from the .txt-file + int* wordCount; + *wordCount = 0; + while (*wordCount < maxWordCount && fgets(buffer, MAX_LINE_LEN, file) != NULL) { + readWordsFromLine(buffer, *wordCount, words, maxWordCount); } - return 0; + return 0; }