43 lines
1.2 KiB
C
43 lines
1.2 KiB
C
#include "input.h"
|
|
#include <string.h>
|
|
#include <ctype.h>
|
|
|
|
// TODO: erledigt
|
|
// 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 buffer[256];
|
|
|
|
// %255s schützt vor Pufferüberläufen des 256-Zeichen-buffers
|
|
if (fscanf(file, "%255s", buffer) == 1)
|
|
{
|
|
strncpy(destination, buffer, maxLen - 1);
|
|
destination[maxLen - 1] = '\0'; // Zwingendes Ende des C-Strings setzen
|
|
|
|
return destination; // Rückgabe bei Erfolg
|
|
}
|
|
return NULL; // Rückgabe bei Dateiende
|
|
}
|
|
|
|
// Read words from file and store in 'words' array
|
|
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;
|
|
} |