Files
I2-Pr/Wordsalat/input.c
T

58 lines
1.3 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)
{
printf("start readWords\n");
int wordcount = 0;
char buffer[256];
char *line = fgets(buffer, sizeof(buffer), file);
while(line != NULL)
{
printf("%d. loop\n", wordcount);
printf("line: %s\n", line);
char* word = strtok(line, " ,;");
while (word != NULL)
{
strncpy(words[wordcount], word, strlen(word));
//printf("%d. word: %s\n", wordcount, words[wordcount]);
wordcount += 1;
word = strtok(NULL, " ,;\t\n");
}
line = fgets(buffer, sizeof(buffer), file);
}
for(int i = 0; i < wordcount; i++)
{
for(int j = 0; j < strlen(words[i]); j++)
{
words[i][j] = toupper(words[i][j]);
if(words[i][j] == '\n')
{
words[i][j] = '\0';
}
}
printf("%d. word: %s\n", i, words[i]);
}
printf("wordcount = %d\n", wordcount);
return wordcount;
}