forked from freudenreichan/info2Praktikum-Wortsalat
input der Wörter
This commit is contained in:
1 parent
b7edd65439
commit
8c747c61fa
1 file changed
+48
-2
+48
-2
@@ -4,9 +4,55 @@
|
|||||||
|
|
||||||
// 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.
|
||||||
|
int splitWords(char texRow[], int maxLength, int wordNumber, char words[][MAX_WORD_LEN], unsigned int maxWordCount);
|
||||||
|
// Read words from file
|
||||||
|
|
||||||
// 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)
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
// 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;
|
||||||
|
}
|
||||||
Reference in new issue
Block a user