generated from freudenreichan/info2Praktikum-Wortsalat
added readwords()
This commit is contained in:
parent
39d08d8609
commit
7245877786
@ -2,11 +2,37 @@
|
||||
#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;
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user