Joel Diaz
Joel Diaz

Reputation: 79

How to store fgets string results into an char array?

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;
    }

Upvotes: 1

Views: 2437

Answers (2)

chux
chux

Reputation: 154243

-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

Upvotes: 2

Mike Andrews
Mike Andrews

Reputation: 3205

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!

Upvotes: 0

Related Questions