Files
Info2-Praktikum/Startcode/input.c
T
2026-10-15 21:13:42 +02:00

32 lines
784 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_WORD_LEN];
char *teiler = " ,;\n";
char *token;
unsigned int wordCount = 0;
fgets(puffer, MAX_LINE_LEN, file);
token = strtok(puffer, 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);
}
return wordCount;
}