Files
Info2-Praktikum/Startcode/input.c
T
2026-10-17 12:43:47 +02:00

43 lines
1005 B
C

#include "input.h"
#include <string.h>
#include <ctype.h>
// 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 puffer[MAX_LINE_NUMBER][MAX_LINE_LEN];
char *teiler = "\n ,;";
char *token;
int i = 0;
unsigned int wordCount = 0;
while(fgets(puffer[i], MAX_LINE_LEN, file) != NULL)
{
i++;
}
i = 0;
token = strtok(puffer[i], teiler);
while(token != NULL && wordCount < maxWordCount)
{
strncpy(words[wordCount], token, MAX_WORD_LEN -1);
//words[wordCount][MAX_WORD_LEN - 1] = '\0';
wordCount++;
token = strtok(NULL, teiler);
if(token == NULL && i < MAX_LINE_NUMBER)
{
token = strtok(puffer[i+1], teiler);
}
}
return wordCount;
}