Hilfsfunktion in input.c
This commit is contained in:
1 parent
213103dad0
commit
def3034ede
1 file changed
+15
-1
+15
-1
@@ -2,8 +2,22 @@
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
|
||||
// TODO:
|
||||
// TODO:
|
||||
// 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]; // Zwischenspeicher zum einlesen
|
||||
if (fgets(buffer, sizeof(buffer), file) != NULL)
|
||||
{
|
||||
buffer[strcspn(buffer, "\r\n")] = '\0'; // Entfernt Umbrüche
|
||||
|
||||
strncpy(destination, buffer, maxLen - 1);
|
||||
destination[maxLen - 1] = '\0'; // Zwingendes Ende des C-Strings setzen, verhindert Pufferüberlauf
|
||||
|
||||
return destination; //Rückgabe bei Erfolg
|
||||
}
|
||||
return NULL; //Rückgabe nur bei Dateiende
|
||||
}
|
||||
|
||||
// Read words from file and store in 'words' array
|
||||
int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
|
||||
|
||||
Reference in new issue
Block a user