I'm just having problems getting strings from a file. The strings are listed one per line and limited by the new line character. I'm trying to store them in a dynamic array of strings and received an invalid next size error and then aborted after the third string.
I'm using a structure Dictionary to store the size in an integer variable and the words in an array of strings.
The code is:
struct Dictionaries {
char** words;
int size;
};
typedef struct Dictionaries dictionary;
dictionary get_dict (FILE* fp) {
dictionary* dict = malloc(sizeof(dictionary));
dict->size = 0;
int strSize = 0;
dict->words = (char**) malloc(sizeof(char*));
dict->words[dict->size] = malloc(sizeof(char));
while(1) {
char c = fgetc(fp);
if (c == EOF ){
break;
}
if (c == '\n') {
dict->words[dict->size][strSize] = '\0';
strSize = 0;
dict->size++;
dict->words[dict->size] = malloc(sizeof(char));
dict->words = (char**) realloc(dict->words, (dict->size+1) * sizeof(char*));
} else {
strSize++;
//printf("%d\n", strSize);
//printf("%d\n", dict->size);
dict->words[dict->size] = (char*) realloc(dict->words[dict->size], (strSize+1) * sizeof(char));
dict->words[dict->size][strSize -1] = c;
}
}
return *dict;
}
PS sorry for the poor formatting, my first time using this site.