generated from freudenreichan/info2Praktikum-Wortsalat
56 lines
1.1 KiB
C
56 lines
1.1 KiB
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)
|
|
{
|
|
/*file = fopen("words.txt", "r"); //liest das doc ein
|
|
if (file == NULL)
|
|
return -1;*/
|
|
FILE *ptr;
|
|
char text [2000];
|
|
char line[200];
|
|
char value;
|
|
int i = 0;
|
|
int start = 0, end= 0;
|
|
|
|
ptr = fopen("words.txt", "r");
|
|
if (ptr == NULL) {
|
|
printf("Datei konnte nicht geöffnet werden.\n");
|
|
return 1;
|
|
}
|
|
|
|
while (fgets(line, sizeof(line), ptr)) {
|
|
for (int j = 0; line[j] != '\0'&& i < sizeof(text)-1;j++) {
|
|
text[i++] = line[j];
|
|
}
|
|
|
|
}
|
|
text[i] = '\0';
|
|
|
|
fclose(ptr);
|
|
|
|
printf("%s\n", text);
|
|
|
|
for (int k = 0 ; k == sizeof(text) || value == '\0' ; k++ ) {
|
|
text[k]=value;
|
|
if ((value == '\n') || (value ==' ') || ( value == ',')|| ( value == ';'))
|
|
{
|
|
end = k-1;
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fclose(ptr);
|
|
|
|
|
|
} |