generated from freudenreichan/info2Praktikum-Wortsalat
31 lines
970 B
C
31 lines
970 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 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);
|
|
}
|
|
|
|
return 0;
|
|
}
|