forked from freudenreichan/info2Praktikum-Wortsalat
Implementiere input.c
This commit is contained in:
1 parent
93db739464
commit
5db3afdc1f
1 file changed
+17
-6
+17
-6
@@ -1,12 +1,23 @@
|
|||||||
#include "input.h"
|
#include "input.h"
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <ctype.h>
|
|
||||||
|
|
||||||
// TODO:
|
int readWord(char *buffer, char *divisor, char words[][MAX_WORD_LEN], unsigned int maxWordCount, int *count);
|
||||||
// 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) {
|
||||||
int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
|
char buffer[MAX_LINE_LEN];
|
||||||
{
|
int count = 0;
|
||||||
|
char *divisor = " ,;.\n";
|
||||||
|
|
||||||
|
while (fgets(buffer, MAX_LINE_LEN, file) != NULL) {
|
||||||
|
for (char *word = strtok(buffer, divisor); word != NULL && count < maxWordCount; word = strtok(NULL, divisor))
|
||||||
|
{
|
||||||
|
strncpy(words[count], word, MAX_WORD_LEN);
|
||||||
|
++count;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return count;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Reference in new issue
Block a user