How to read text file to struct type array

0

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!

c
asked on Stack Overflow Apr 28, 2021 by altF4 • edited May 2, 2021 by Mark Rotteveel

2 Answers

3

Q: How to read text file to struct type array?

A: You're doing everything right: allocate space for the array, open your file and read a line at a time, parse the data from the line and read it into the next array element. Good :)

Q: How do I prevent 0xC0000005 (segmentation violation)?

A: You need to allocate space for the STRINGS in your structs!

EXAMPLE:

#define LINESIZE 100
#define MAX_STRING 80

typedef struct
{
    char word[MAX_STRING]; //word and corresponding hint
    char clue[MAX_STRING];
    int x; //Starting x and y positions
    int y;
    char direction; //H for horizontal, V for vertical
    int f; //solved or not
} Word_t;

ALSO:

  • &arr[i].direction is correct, but...
  • The format should be %c, not %s

EXAMPLE:

val = sscanf(buffer, "%c %d %d %s %[^\n]", &arr[i].direction, &arr[i].x, &arr[i].y, arr[i].word, arr[i].clue);
answered on Stack Overflow Apr 28, 2021 by paulsm4 • edited Apr 28, 2021 by paulsm4
2
  • You must allocate buffer and assign to word and clue before reading strings there.
  • %s is for reading strings (null-terminated sequence of characters), so it cannot be used for read one character to char variable direction. You should use %c for that. You can add a whitespace before that to have sscanf() skip whitspace characters.
  • Using %s to print one character is also wrong. %s in printf() is for printing strings.
  • Also you should limit the maximum length to read to prevent buffer overrun.
  • Casting results of malloc() family is considered as a bad practice.
/* allocate memory */
arr[i].word = malloc(10240);
arr[i].clue = malloc(10240);
/* use correct format specifiers */
val = sscanf(buffer, " %c %d %d %10239s %10239[^\n]", &arr[i].direction, &arr[i].x, &arr[i].y, arr[i].word, arr[i].clue);
printf("print = %c %d %d %s %s\n", arr[i].direction, arr[i].x, arr[i].y, arr[i].word, arr[i].clue);
answered on Stack Overflow Apr 28, 2021 by MikeCAT

User contributions licensed under CC BY-SA 3.0