37 lines
988 B
C
37 lines
988 B
C
#include "input.h"
|
|
#include <string.h>
|
|
#include <ctype.h>
|
|
#include <stdlib.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)
|
|
{
|
|
int count = 0;
|
|
char *line = calloc(MAX_LINE_LEN, sizeof(char));
|
|
|
|
char delimit[] = ",.;:!? \n";
|
|
|
|
while(fgets(line, MAX_LINE_LEN, file) != NULL && (count < maxWordCount)){
|
|
|
|
char *token = strtok(line, delimit);
|
|
|
|
while(token != NULL){
|
|
size_t len = strlen(token);
|
|
token[len] = '\0';
|
|
char copy[MAX_LINE_LEN];
|
|
strncpy(copy, token, len);
|
|
copy[len] = '\0';
|
|
|
|
strncpy(words[count], copy, len);
|
|
words[count][len] = '\0';
|
|
count++;
|
|
|
|
token = strtok(NULL, delimit);
|
|
}
|
|
}
|
|
|
|
return count;
|
|
} |