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
+8 -6
+8 -6
View File
@@ -13,16 +13,18 @@ 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;
while (count < maxWordCount && (ch = fgetc(file)) != EOF) { while (count < maxWordCount && (ch = fgetc(file)) != EOF) {
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,10 +45,11 @@ 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++;
} }
return (int)count; return (int)count;
} }