I have an assignment for C and trying to solve it for hours and have not succeeded yet.
So I need to read a text file and append its content to struct type array,
text file =>
5 5 7
H 1 1 MILK White liquid produced by the mammals
H 2 1 IN Used to indicate inclusion within space, a place, or limits
H 3 3 BUS A road vehicle designed to carry many passengers
this is the code =>
#define LINESIZE 100
typedef struct
{
char *word; //word and corresponding hint
char *clue;
int x; //Starting x and y positions
int y;
char direction; //H for horizontal, V for vertical
int f; //solved or not
} Word_t;
Word_t* loadTextFile(FILE* file, int numofWords) {
//allocate memory for dynamic array
Word_t *arr = (Word_t*) malloc(sizeof (Word_t) * numOfWords);
char buffer[LINESIZE];
int i = -1; //to skip reading first line, I read it somewhere else
int val;
if (file != NULL) {
while(fgets(buffer, sizeof(buffer), file) != NULL) {
if (i > -1) {
printf("buffer: %s", buffer);
val = sscanf(buffer, "%s %d %d %s %[^\n]", &arr[i].direction, &arr[i].x, &arr[i].y, arr[i].word, arr[i].clue);
printf("print = %s %d %d %s %s\n", &arr[i].direction, arr[i].x, arr[i].y, arr[i].word, arr[i].clue);
}
if (val != 5 && i > -1)
printf("something wrong");
i++;
}
}
fclose(file);
return arr;
}
After I run the code, I get an output like this
buffer: H 1 1 MILK White liquid produced by the mammals
print = H 1 1 MILK White liquid produced by the mammals
buffer: H 2 1 IN Used to indicate inclusion within space, a place, or limits
Process finished with exit code -1073741819 (0xC0000005)
This error code means I have issues with pointers and memory, I think I have a problem with dynamic type array but I can't solve it. Help me please!