Worteinlesen mit strncpy angepasst

This commit is contained in:
Mihaela Creciun committed 2026-10-21 13:55:13 +02:00
1 parent a1a0b615a7
commit f8deef43e1
1 file changed
+6 -4
+6 -4
View File
@@ -13,6 +13,7 @@ int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
} }
unsigned int count = 0; unsigned int count = 0;
char currentWord[MAX_WORD_LEN];
size_t length = 0; size_t length = 0;
int tooLong = 0; int tooLong = 0;
int ch; int ch;
@@ -22,7 +23,8 @@ int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
if (ch == ',' || ch == ';' || isspace((unsigned char)ch)) { if (ch == ',' || ch == ';' || isspace((unsigned char)ch)) {
if (length > 0 && !tooLong) { if (length > 0 && !tooLong) {
words[count][length] = '\0'; currentWord[length] = '\0';
strncpy(words[count], currentWord, MAX_WORD_LEN);
count++; count++;
} }
@@ -32,8 +34,7 @@ int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
else if (!tooLong) { else if (!tooLong) {
if (length < MAX_WORD_LEN - 1) { if (length < MAX_WORD_LEN - 1) {
words[count][length] = currentWord[length] = (char)toupper((unsigned char)ch);
(char)toupper((unsigned char)ch);
length++; length++;
} }
else { else {
@@ -44,7 +45,8 @@ int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
} }
if (count < maxWordCount && length > 0 && !tooLong) { if (count < maxWordCount && length > 0 && !tooLong) {
words[count][length] = '\0'; currentWord[length] = '\0';
strncpy(words[count], currentWord, MAX_WORD_LEN);
count++; count++;
} }