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

@ -7,35 +7,39 @@
// Read words from file and store in 'words' array
// f gets ; komma und semikolon erkennen und alles in groß buchstaben (=uppercase) (strtok)
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 string[MAX_WORD_LEN];
char stringcopy[MAX_WORD_LEN];
char *stringptr;
unsigned int count = 0;
int shouldClose = 0;
file = fopen("words.txt", "r");
if (file == NULL)
{
printf("Fehler: Datei konnte nicht geöffnet werden!\n");
return 1;
file = fopen("words.txt", "r");
if (file == NULL)
{
printf("Fehler: Datei konnte nicht geöffnet werden!\n");
return 1;
}
shouldClose = 1;
}
else
while (fgets(words[count], MAX_WORD_LEN, file) != NULL && count < maxWordCount)
{
printf("Datei konnte geöffnet werden");
fgets(string, MAX_WORD_LEN, file);
strcpy(stringcopy, string);
stringptr = strtok(stringcopy, delimiter);
while (stringptr != NULL){
stringptr = strtok(NULL, delimiter);
maxWordCount ++;
}
char* token = strtok(words[count], delimiter);
while (token != NULL && count < maxWordCount)
{
for (char* p = token; *p; ++p)
(char)toupper((unsigned char)*p);
strncpy(words[count], token, MAX_WORD_LEN - 1);
words[count][MAX_WORD_LEN - 1] = '\0';
count++;
token = strtok(NULL, delimiter);
}
}
if (shouldClose)fclose(file);
return(int)count;
}