Finished input.c V1, starting test and fix phase

This commit is contained in:
Lukas Weber 2025-11-04 19:54:04 +01:00
parent 078e7fcf44
commit f036995e78

View File

@ -1,30 +1,39 @@
#include "input.h"
#include <string.h>
#include <ctype.h>
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;
}