1

I am currently getting the following error

Process terminated with status -1073741819 

and I suspect its my fgets() but I have no idea why this is happening, any help would be much appreciated.

 //Gets Dictionary from file
    char* GetDictionary() {
        int ArraySize;
        int i = 0;
        FILE * DictionaryFile;

        //Gets first line (in this case it is the amount of Lines)
        DictionaryFile = fopen("dictionary.txt", "r");
        fscanf(DictionaryFile,"%d", &ArraySize);
        ArraySize = ArraySize + 1;
        printf("%d", ArraySize);
        fclose(DictionaryFile);

        //Gets the array
        char* Dictionary = malloc(sizeof(char)*ArraySize);
        char Temp[ArraySize];
        char TempArray[ArraySize];

        DictionaryFile = fopen("dictionary.txt", "r");
        while(fgets(Temp, sizeof Temp, DictionaryFile)!=NULL) {
          Dictionary[i] = Temp;
          //Check The array
          printf("%s", Dictionary[i]);
          i++;
        }

        fclose(DictionaryFile);
        return Dictionary;
    }
3
  • 3
    Equally Bad: Dictionary[i] = Temp; - if that doesn't spew a warning from your compiler, you seriously need to crank up the warning level. That tries to save a char* to a char. And printf("%s", Dictionary[i]); isn't any better, sending a char promoted to int to a format specifier expecting char*. Commented Sep 4, 2015 at 20:55
  • Closing the file and reopening it resets the file pointer to the beginning, so you'll read the array size again. I'm guessing you don't want that. Also, you should check that Dictionary != NULL after calling fopen. Failure to do so will result in a crash, if fopen wasn't able to open the file. Commented Sep 4, 2015 at 20:59
  • Does ArraySize represent the number of remaining characters or the number of remaining lines of text? Commented Sep 4, 2015 at 21:15

2 Answers 2

2

-1073741819 --> C0000005 and likely has some significance. Maybe use below to discern its meaning.

puts(strerror(-1073741819));

Code has many issues: Here are some corrected to get you going.

1) Allocate an array of pointers, not an array of char

// char* Dictionary = malloc(sizeof(char)*ArraySize);
char** Dictionary = malloc(ArraySize * sizeof *Dictionary);

2) Form a big buffer to read each line

 char Temp[100];

3) After reading each line, get rid of the likely trailing '\n'

 size_t len = strlen(Temp);
 if (len && Temp[len-1] == '\n') Temp[--len] = 0;

4) Allocate memory for that word and save

 Dictionary[i] = malloc(len + 1);
 assert(Dictionary[i]);
 memcpy(Dictionary[i], Temp, len + 1);

5) Robust code frees it allocations before completion

6) Code reads "amount of Lines" twice as file is opened twice. Just leave file open (and not re-open it). @user3386109

Sign up to request clarification or add additional context in comments.

Comments

0

You likely want Dictionary to be an array of char strings. That is, Dictionary is an array, and each element in the array is a char *. That makes Dictionary a char **.

For this example, it may be most straightforward to allocate memory for the Dictionary array itself, then allocate memory for its contents. You'll need to free all this when you're done, of course.

char **Dictionary = malloc(sizeof(char *) * ArraySize);
for (int i = 0; i < ArraySize; i++) {
    Dictionary[i] = malloc(ArraySize);
}

There are better ways to do this. For one, you might only allocate memory when you need it, for each fgets() return. You could also use strdup() to allocate only the memory you need. You could also pass in Dictionary from the caller, already allocated, so you don't worry about allocating it here.

Later in your program, as @WhozCraig pointed out, you need to copy the string in Temp, like strcpy(Dictionary[i], Temp), in place of Dictionary[i] = Temp. I too am surprised that's not generating a compiler warning!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.