added kristin input.c code
This commit is contained in:
parent
7f69b71455
commit
d269bc321b
1
.gitignore
vendored
1
.gitignore
vendored
@ -2,3 +2,4 @@ makefile/
|
|||||||
.vscode/
|
.vscode/
|
||||||
*.exe
|
*.exe
|
||||||
Start_Linux/makefile
|
Start_Linux/makefile
|
||||||
|
Start_Windows/makefile
|
||||||
|
|||||||
@ -1,12 +1,54 @@
|
|||||||
#include "input.h"
|
#include "input.h"
|
||||||
#include <string.h>
|
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
// 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) {
|
||||||
|
|
||||||
|
if (file == NULL) {
|
||||||
|
|
||||||
|
perror("File couldn't be opened");
|
||||||
|
}
|
||||||
|
|
||||||
|
// zunächst fehlerhaften String einlesen und in anderem Array
|
||||||
|
// zwischenspeichern
|
||||||
|
|
||||||
|
char fehlerhafterString[MAX_LINE_LEN];
|
||||||
|
char *teiler = " ;,.\n";
|
||||||
|
char *aktuellesWort;
|
||||||
|
int wortAnzahl = 0;
|
||||||
|
|
||||||
|
while (fgets(fehlerhafterString, sizeof(fehlerhafterString), file) !=
|
||||||
|
NULL && // während mit fgets alle Zeichen aus dem file eingelesen
|
||||||
|
// werden
|
||||||
|
wortAnzahl < maxWordCount) {
|
||||||
|
|
||||||
|
aktuellesWort =
|
||||||
|
strtok(fehlerhafterString, teiler); // Erstes Wort mit strtok aus dem
|
||||||
|
// fehlerhaften String herauslösen
|
||||||
|
|
||||||
|
while (aktuellesWort != NULL &&
|
||||||
|
wortAnzahl < maxWordCount) { // while strtok nicht am Ende ist und
|
||||||
|
// noch Wörter in words passen
|
||||||
|
|
||||||
|
strncpy(words[wortAnzahl], aktuellesWort,
|
||||||
|
sizeof(words[wortAnzahl]) -
|
||||||
|
1); // mit strcpy das aktuelle Wort in words kopieren
|
||||||
|
words[wortAnzahl][sizeof(words[wortAnzahl]) - 1] =
|
||||||
|
'\0'; // Nullterminator mit sizeof des aktuellen Worts - 1 an Ende des
|
||||||
|
// Worts setzen
|
||||||
|
|
||||||
|
wortAnzahl++; // Nächstes Wort
|
||||||
|
aktuellesWort = strtok(NULL, teiler); // Nächstes Token
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return wortAnzahl; // Anzahl der eingelesenen Wörter
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Loading…
x
Reference in New Issue
Block a user