Hallo Leute Ich versuche eine Rechtschreibprüfung für einige Kursarbeit zu implementieren, aber ich bin neu in C und nur das Laden der Wörterbuchdatei ruiniert meinen Kopf. Der folgende Code wird zwar kompiliert, stürzt jedoch ab, wenn er ausgeführt wird. Manchmal nach dem Drucken von 500 Zeilen manchmal 1500, aber ich habe keine Ahnung, was könnte es verursachen!Wörterbuchdatei stürzt ab, aber kompiliert
#include <string.h>
#include <stdio.h>
#include <malloc.h>
int main(int argc, char *argv[])
{
FILE *words_ptr; //pointer for words.txt
char new_word[100];
char temp_word[100];
char *dict[45440];
words_ptr = fopen("dictionary.txt", "r");
if(words_ptr != NULL)
{
printf("File dictionary.txt opened\n");
int i = 0;
while (fgets(temp_word, 45440, words_ptr))
{
new_word[i] = (char)calloc(strlen(temp_word), sizeof(char)); //ensuring new_word will be the right size
strcpy(new_word, temp_word); //copy contents of temp_word to new_word
dict[i] = new_word; //copy contents of new_word to i'th element of dict array
printf("printing out dict[%d]: %s\n", i, dict[i]);
i++;
}
printf("printing out dictionary1: %s\n", dict[1]);
fclose(words_ptr);
return 0;
}
else {printf("Unable to open file words.txt\n"); return 1;}
}
* neues_wort [i] = (char) calloc (strlen (temp_wort), sizeof (char)); // sicherstellen, dass new_word die richtige Größe hat * Nicht korrekt zitiert. 'new_word' ist ein Byte zu klein - Sie haben den Platz für das abschließende' \ 0''-Zeichen vergessen. –
Hinweis: Wirf das Ergebnis von 'calloc' & friends oder' void * 'nicht generell in C. – Olaf
' fgets (temp_word, 45440, words_ptr) '' 'fgets (temp_word, 100, words_ptr)' –