forked from freudenreichan/info2Praktikum-Wortsalat
Worteinlesen mit strncpy angepasst
This commit is contained in:
1 parent
a1a0b615a7
commit
f8deef43e1
1 file changed
+8
-6
+8
-6
@@ -13,16 +13,18 @@ int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
|
||||
}
|
||||
|
||||
unsigned int count = 0;
|
||||
char currentWord[MAX_WORD_LEN];
|
||||
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';
|
||||
currentWord[length] = '\0';
|
||||
strncpy(words[count], currentWord, MAX_WORD_LEN);
|
||||
count++;
|
||||
}
|
||||
|
||||
@@ -32,8 +34,7 @@ int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
|
||||
else if (!tooLong) {
|
||||
|
||||
if (length < MAX_WORD_LEN - 1) {
|
||||
words[count][length] =
|
||||
(char)toupper((unsigned char)ch);
|
||||
currentWord[length] = (char)toupper((unsigned char)ch);
|
||||
length++;
|
||||
}
|
||||
else {
|
||||
@@ -44,10 +45,11 @@ int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
|
||||
}
|
||||
|
||||
if (count < maxWordCount && length > 0 && !tooLong) {
|
||||
words[count][length] = '\0';
|
||||
currentWord[length] = '\0';
|
||||
strncpy(words[count], currentWord, MAX_WORD_LEN);
|
||||
count++;
|
||||
}
|
||||
|
||||
return (int)count;
|
||||
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user