Hauptfunktion und Anpassung Hilfsfunktion in input.c
This commit is contained in:
1 parent
6c5710f2f1
commit
361e5ddaca
1 file changed
+26
-9
+26
-9
@@ -2,25 +2,42 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
|
|
||||||
// TODO:
|
// TODO: erledigt
|
||||||
// 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.
|
||||||
char* readSingleWord(FILE *file, char *destination, int maxLen)
|
char* readSingleWord(FILE *file, char *destination, int maxLen)
|
||||||
{
|
{
|
||||||
char buffer[256]; // Zwischenspeicher zum einlesen
|
char buffer[256];
|
||||||
if (fgets(buffer, sizeof(buffer), file) != NULL)
|
|
||||||
|
// %255s schützt vor Pufferüberläufen des 256-Zeichen-buffers
|
||||||
|
if (fscanf(file, "%255s", buffer) == 1)
|
||||||
{
|
{
|
||||||
buffer[strcspn(buffer, "\r\n")] = '\0'; // Entfernt Umbrüche
|
|
||||||
|
|
||||||
strncpy(destination, buffer, maxLen - 1);
|
strncpy(destination, buffer, maxLen - 1);
|
||||||
destination[maxLen - 1] = '\0'; // Zwingendes Ende des C-Strings setzen, verhindert Pufferüberlauf
|
destination[maxLen - 1] = '\0'; // Zwingendes Ende des C-Strings setzen
|
||||||
|
|
||||||
return destination; //Rückgabe bei Erfolg
|
return destination; // Rückgabe bei Erfolg
|
||||||
}
|
}
|
||||||
return NULL; //Rückgabe nur bei Dateiende
|
return NULL; // Rückgabe bei Dateiende
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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)
|
||||||
{
|
{
|
||||||
|
unsigned int count = 0;
|
||||||
|
|
||||||
|
while (count < maxWordCount)
|
||||||
|
{
|
||||||
|
// Aufruf Hilfsfunktion mit Arraygröße als Parameter
|
||||||
|
if (readSingleWord(file, words[count], MAX_WORD_LEN) != NULL)
|
||||||
|
{
|
||||||
|
if (strlen(words[count]) > 0)
|
||||||
|
{
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
break; // Ende der Datei erreicht
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return count;
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user