Files
Wortsalat/Startcode/input-t.c
T

29 lines
889 B
C

//Torbens Kopie
#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 *tmp;
int counter = 0;
const char delimiters[] = ",!.;:? "
while(fgets(tmp, MAX_WORD_LEN, flie) != NULL){
char *token = strtok_r (tmp, delimiters, &delimiters);
if(strncpy(words[counter], token, MAX_WORD_LEN) == NULL){
//failure
return -1;
}
counter++;
while(token != NULL){
token = strtpk_r (NULL, delimiters, &delimiters);
strncpy(words[counter], token, MAX_WORD_LEN);
counter++;
}
}
return counter;
}