generated from freudenreichan/info2Praktikum-Wortsalat
Input.c Update
This commit is contained in:
parent
b9d2a90e29
commit
0da01c3a15
Binary file not shown.
@ -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.
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user