forked from freudenreichan/info2Praktikum-Wortsalat
59 lines
1.5 KiB
C
59 lines
1.5 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.
|
|
int splitWords(char texRow[], int maxLength, int wordNumber, char words[][MAX_WORD_LEN], unsigned int maxWordCount);
|
|
// Read words from file
|
|
|
|
int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
|
|
{
|
|
|
|
// Datei Zeilenweise auslesen
|
|
char textRow[MAX_LINE_LEN];
|
|
int row = 0;
|
|
|
|
while (fgets(textRow, MAX_LINE_LEN, file) != NULL)
|
|
row = splitWords(textRow, MAX_LINE_LEN, row, words, maxWordCount);
|
|
|
|
fclose(file);
|
|
|
|
for (int i; i < row; i++)
|
|
printf("%s\n", words[i]);
|
|
|
|
return row;
|
|
}
|
|
|
|
// Zeilen in lesbare Wörter aufteilen und "words" zuweisen
|
|
int splitWords(char textRow[], int maxStringLength, int row, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
|
|
{
|
|
char *teiler = " .,\n;";
|
|
char *token = strtok(textRow, teiler);
|
|
|
|
if (token != NULL)
|
|
{
|
|
if (row < maxWordCount)
|
|
{
|
|
strncpy(words[row], token, maxStringLength);
|
|
words[row][maxStringLength - 1] = '\0';
|
|
row++;
|
|
}
|
|
}
|
|
|
|
while (token != NULL && row < maxWordCount)
|
|
{
|
|
|
|
token = strtok(NULL, teiler);
|
|
|
|
if (token != NULL)
|
|
{
|
|
strncpy(words[row], token, maxStringLength);
|
|
words[row][maxStringLength - 1] = '\0';
|
|
row++;
|
|
}
|
|
}
|
|
|
|
return row;
|
|
}
|