generated from freudenreichan/info2Praktikum-Wortsalat
Compare commits
No commits in common. "771cc1706f9ec25dca0ca3e0461a64b1cf19154f" and "f7a6e56257a2dfe40245580aa2d2c2f7fb086fb8" have entirely different histories.
771cc1706f
...
f7a6e56257
@ -2,37 +2,11 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
|
|
||||||
#define MAX_BUFFER_LEN 256
|
|
||||||
|
|
||||||
// TODO:
|
// TODO:
|
||||||
// eine Funktion implementieren, die ein einzelnes Wort aus einer Textdatei (words.txt) einliest und als C-String zurückgibt.
|
// 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
|
// Read words from file and store in 'words' array
|
||||||
int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
|
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