input.c verbessert

This commit is contained in:
Harun Faizi 2025-11-02 14:31:20 +01:00
parent 78d9694821
commit 476887d5ab

21
input.c
View File

@ -18,7 +18,12 @@
// Read words from file and store in 'words' array // Read words from file and store in 'words' array
int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount) int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
{ { //Gibt die Anzahl der eingelesen Wörter zurück (wordCount), für main wie viel gelesen wurde
//FILE *file: Zeiger auf Datei --> FILE komplexes Objekt, müssen mit Adresse arbeiten
//Wort = String = Array von Zeichen (char) --> Zeilen: Wörteranzahl und Spalten: Buchstaben der Wörter
//wie viele Wörter max in Array geschrieben werden dürfen
if (file == 0) if (file == 0)
return 0; //Sicherheitsprüfung return 0; //Sicherheitsprüfung
@ -29,7 +34,7 @@ int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
while(fgets(line, sizeof(line), file) != NULL) while(fgets(line, sizeof(line), file) != NULL)
{ {
// Zerlege die Datei in einzelne Wörter // Zerlege die Datei in einzelne Wörter
char *token = strtok(line, ",;\n\t"); char *token = strtok(line, ".,;\n\t");
while (token != NULL && wordCount < maxWordCount) while (token != NULL && wordCount < maxWordCount)
{ {
@ -38,24 +43,12 @@ int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
words[wordCount][MAX_WORD_LEN -1] = '\0'; words[wordCount][MAX_WORD_LEN -1] = '\0';
// Entferne führende Leerzeichen
int start = 0;
while (words[wordCount][start] == ' ')
start++;
if (start > 0)
memmove(words[wordCount], words[wordCount] + start, strlen(words[wordCount]) - start + 1);
// Entferne nachfolgende Leerzeichen
int end = strlen(words[wordCount]) - 1;
while (end >= 0 && words[wordCount][end] == ' ')
{
words[wordCount][end] = '\0'; words[wordCount][end] = '\0';
end--; end--;
} }
wordCount++; wordCount++;
token = strtok(NULL, ",;\n"); token = strtok(NULL, ",;\n");
} }
// Wenn max Wortzahl erreicht ist abbrechen! // Wenn max Wortzahl erreicht ist abbrechen!