Input.c Update

This commit is contained in:
Thomas Rauh Desktop 2025-10-29 20:47:36 +01:00
parent b9d2a90e29
commit 0da01c3a15
5 changed files with 35 additions and 0 deletions

Binary file not shown.

View File

@ -5,8 +5,43 @@
// TODO: // TODO:
// eine Funktion implementieren, die ein einzelnes Wort aus einer Textdatei (words.txt) einliest und als C-String zurückgibt. // eine Funktion implementieren, die ein einzelnes Wort aus einer Textdatei (words.txt) einliest und als C-String zurückgibt.
// 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)
{ {
char zeile[MAX_LINE_LEN];
char inhalt[MAX_WORD_LEN*maxWordCount];
char *trenner = " .;,";
char *token;
char *enter = "\n";
//Zeilen auslesen und als ein String speichern
while(fgets(zeile,MAX_LINE_LEN, file) != NULL){
if (strstr(zeile, enter) != NULL){
zeile[strlen(zeile)-1] = ';';
}
strncat(inhalt, zeile, strlen(zeile));
}
//erstes Wort finden
token = strtok(inhalt, trenner);
//Woerter auftrennen und in words[][] speichern
int counter = 0;
while(token != NULL){
strncpy(words[counter], token, MAX_WORD_LEN-1);
words[counter][MAX_WORD_LEN - 1] = '\0'; //Sicherstellen von Stringende
token = strtok(NULL, trenner);
counter ++;
}
// Datei schliessen
fclose(file);
//Test
//for(int i =0;i<counter;i++){
// printf("%s\n", words[i]);
//}
return counter;
} }

Binary file not shown.

Binary file not shown.