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