forked from freudenreichan/info2Praktikum-Wortsalat
Implementierung der Worteinlesefunktion
This commit is contained in:
1 parent
60bb394fb7
commit
a1a0b615a7
1 file changed
+41
@@ -8,5 +8,46 @@
|
|||||||
// Read words from file and store in 'words' array
|
// 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)
|
||||||
{
|
{
|
||||||
|
if (file == NULL || words == NULL || maxWordCount == 0) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned int count = 0;
|
||||||
|
size_t length = 0;
|
||||||
|
int tooLong = 0;
|
||||||
|
int ch;
|
||||||
|
|
||||||
|
while (count < maxWordCount && (ch = fgetc(file)) != EOF) {
|
||||||
|
|
||||||
|
if (ch == ',' || ch == ';' || isspace((unsigned char)ch)) {
|
||||||
|
|
||||||
|
if (length > 0 && !tooLong) {
|
||||||
|
words[count][length] = '\0';
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
|
||||||
|
length = 0;
|
||||||
|
tooLong = 0;
|
||||||
|
}
|
||||||
|
else if (!tooLong) {
|
||||||
|
|
||||||
|
if (length < MAX_WORD_LEN - 1) {
|
||||||
|
words[count][length] =
|
||||||
|
(char)toupper((unsigned char)ch);
|
||||||
|
length++;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
tooLong = 1;
|
||||||
|
length = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (count < maxWordCount && length > 0 && !tooLong) {
|
||||||
|
words[count][length] = '\0';
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
|
||||||
|
return (int)count;
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user