input . c erster test

This commit is contained in:
Luis Gessnitzer 2025-11-04 10:29:48 +01:00
parent 405ffbd805
commit 89ac27f8d3

View File

@ -10,32 +10,36 @@
int readWords(FILE* file, char words[][MAX_WORD_LEN], unsigned int maxWordCount) int readWords(FILE* file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
{ {
char delimiter[] = ", ; \n"; char delimiter[] = ", ; \n";
char string[MAX_WORD_LEN]; unsigned int count = 0;
char stringcopy[MAX_WORD_LEN]; int shouldClose = 0;
char *stringptr;
if (file == NULL)
{
file = fopen("words.txt", "r"); file = fopen("words.txt", "r");
if (file == NULL) if (file == NULL)
{ {
printf("Fehler: Datei konnte nicht geöffnet werden!\n"); printf("Fehler: Datei konnte nicht geöffnet werden!\n");
return 1; return 1;
} }
else shouldClose = 1;
}
while (fgets(words[count], MAX_WORD_LEN, file) != NULL && count < maxWordCount)
{ {
printf("Datei konnte geöffnet werden"); char* token = strtok(words[count], delimiter);
fgets(string, MAX_WORD_LEN, file); while (token != NULL && count < maxWordCount)
strcpy(stringcopy, string); {
stringptr = strtok(stringcopy, delimiter); for (char* p = token; *p; ++p)
while (stringptr != NULL){ (char)toupper((unsigned char)*p);
stringptr = strtok(NULL, delimiter); strncpy(words[count], token, MAX_WORD_LEN - 1);
maxWordCount ++; words[count][MAX_WORD_LEN - 1] = '\0';
count++;
}
token = strtok(NULL, delimiter);
} }
} }
if (shouldClose)fclose(file);
return(int)count;
}