generated from freudenreichan/info2Praktikum-Wortsalat
Continued working on input.c, Version 1, still no testing for functionability
This commit is contained in:
+20
-1
@@ -4,8 +4,27 @@
|
||||
|
||||
// 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)
|
||||
{
|
||||
char buffer[101]; // prepares a large enough buffer to read one line from the .txt-file
|
||||
char *wordseperator = " ,;"; // sets up checks for seperated words
|
||||
char *token;
|
||||
int readwords = 0;
|
||||
|
||||
while (readwords < maxWordCount) {
|
||||
|
||||
do {
|
||||
token = strtok(buffer, wordseperator);
|
||||
} while (*token == ' ' || *token == ',' || *token == ';');
|
||||
if (strlen(token) <= MAX_WORD_LEN) {
|
||||
for (int i = 0; i < strlen(token); i++) {
|
||||
words[readwords][i] = token[i];
|
||||
}
|
||||
readwords++;
|
||||
}
|
||||
token = strtok(NULL, wordseperator);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user