-1

I am currently having a segmentation fault 11 in this code here. I am trying to read an input file and store it into my structure. Whenever I run, it only prints out the first item of the text file, then it gives me a Segmentation Fault 11 error. What could be causing this?

Here is my code:

    typedef struct{
    int serialNum;
    char giftName[50];
    int price;
    char store[50];
} product;

void read_File(const char *filename)
{
    filename = "items.txt";
    product p[4] = {0};
    int i = 0;
    FILE *file = fopen(filename, "r");
    if (file){
        char line[100];
        while(fgets(line, sizeof line, file) && i < 6){
            fputs(line, stdout);
            if (sscanf(line, "%d %s %d %s", 
                p[i].serialNum,
                p[i].giftName,
                p[i].price,
                p[i].store) == 5)
                {
                    printf("Storing information in structure complete.");
                }
        }
    
    fclose(file);
}
else{
    perror(filename);
}
}
  • Why is your limit `i < 6` (i.e. `i <= 5`) when `p` has only `4` members? – Dai Feb 21 '21 at 14:40
  • 2
    Several issues here. Your `while` loop never increments `i` so its value is always 0. You've allocated space for 4 products, but your `i < 6` suggests you'll allow up to 6. You are checking `sscanf` for result of 5, but you are only scanning for 4 items. – lurker Feb 21 '21 at 14:41
  • 1
    And actually reading of unlimited lines is allowed because `i` is not changed. – MikeCAT Feb 21 '21 at 14:43
  • Just one tip, don't use `sscanf()` as a condition. It makes debugging harder and the `if` much less readable – Jack Lilhammers Feb 21 '21 at 14:45
  • You're doing 4 assignments with `sscanf`. Why are you comparing the result with 5? – klutt Feb 21 '21 at 15:12

1 Answers1

1

To read integers via sscanf() (or other scanf() family), you have to pass pointers so that sscanf() can write the integers read using the pointers.

On the other hand, to read strings via sscanf(), you need not and shouldn't add & because the arrays will be automatically converted to the pointers to the first elements.

Also it is weird to check == 5 while there are only 4 things to read.

            if (sscanf(line, "%d %s %d %s", 
                &p[i].serialNum, /* add & to pass pointer */
                p[i].giftName,
                &p[i].price, /* add & to pass pointer */
                p[i].store) == 4) /* correct number */
MikeCAT
  • 73,922
  • 11
  • 45
  • 70