generated from freudenreichan/info2Praktikum-Wortsalat
38 lines
917 B
C
38 lines
917 B
C
#include "input.h"
|
|
#include <string.h>
|
|
#include <ctype.h>
|
|
|
|
#define MAX_BUFFER_LEN 256
|
|
|
|
// 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[MAX_BUFFER_LEN];
|
|
int count = 0;
|
|
char *teiler = ".,; !?\n";
|
|
|
|
if(file == NULL)
|
|
{
|
|
printf("Fehler beim Öffnen von words.txt!\n");
|
|
return -1;
|
|
}
|
|
|
|
while(fgets(buffer, MAX_LINE_LEN, file))
|
|
{
|
|
const char *wort = strtok(buffer, teiler);
|
|
|
|
while(wort != NULL && count < maxWordCount)
|
|
{
|
|
strncpy(words[count], wort, MAX_WORD_LEN-1);
|
|
words[count][MAX_WORD_LEN-1] = '\0';
|
|
count++;
|
|
|
|
wort = strtok(NULL, teiler);
|
|
}
|
|
}
|
|
|
|
return count;
|
|
} |