generated from freudenreichan/info2Praktikum-Wortsalat
51 lines
1.8 KiB
C
51 lines
1.8 KiB
C
#include "input.h"
|
|
#include <string.h>
|
|
#include <ctype.h>
|
|
|
|
// TODO:
|
|
// 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
|
|
int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
|
|
{
|
|
puts("....");
|
|
char puffer[MAX_LINE_LEN]; //Array für eingelesene Zeile
|
|
int counter = 0; //Zähler für Anzahl eingelesener Wörter
|
|
int i;
|
|
|
|
while(fgets(puffer, MAX_LINE_LEN-1, file) != NULL && counter < maxWordCount)
|
|
{
|
|
puts(".!!!!!");
|
|
char *word_parts = strtok(puffer, ",;\n\t/. "); //Wörter aufteilen
|
|
|
|
while(word_parts != NULL && counter < maxWordCount)
|
|
{
|
|
// Kopiere das Wort in words-Array
|
|
strncpy(words[counter], word_parts, MAX_WORD_LEN - 1);
|
|
words[counter][MAX_WORD_LEN - 1] = '\0';
|
|
|
|
// Alles in Großbuchstaben umwandeln
|
|
for(i = 0; words[counter][i] != '\0'; i++)
|
|
{
|
|
words[counter][i] = toupper(words[counter][i]);
|
|
printf("%c",words[counter][i])
|
|
}
|
|
|
|
/*
|
|
for(i = 0; i < MAX_WORD_LEN -1; i++) //MAX = 100
|
|
{
|
|
words[counter][i] = toupper(word_parts[i]); //Großbuchstaben erzeugen
|
|
puts("?????????????????");
|
|
printf("%c", word_parts[i]);
|
|
if(word_parts[i+1] == '\0')
|
|
break;
|
|
}
|
|
words[counter][i] = '\0'; //Stringdefinition vervollständigen
|
|
*/
|
|
counter++; // Wort eingelesen, Wortzähler erhöhen
|
|
word_parts = strtok(NULL, ",;\n\t/. "); //NULL für Zeiger angeben
|
|
}
|
|
|
|
}
|
|
return counter;
|
|
} |