-3

I don't know why but this creates segmentation fault. I don't think the problem is in the code for reading in a file and editing a structure. The head is a dummy node. I don't know what is the problem and it frustrates me. Can someone help me understand?

FOOD *New;
FILE *fp = fopen(strcat(fileName,".txt"), "r");
if(fp==NULL)    {
    printf("File %s is not found.\n", strcat(fileName,".txt"));
    return;
}
while(!feof(fp)){
    New = (FOOD*)malloc(sizeof(FOOD));

    for(i = 0; i < 9; i++){

        if(i == 0||i==1||i==2){
            fgets(scan, 256, fp);
            if(i==0)    strcpy(New->category, scan);
            else if (i==1)  strcpy(New->itemDescription, scan);
            else    strcpy(New->itemUnit, scan);
            //printf("NO");
        }

        else if (i==3||i==4||i==5){
            fscanf(fp, "%f", &fscan);
            fgetc(fp);
            if(i==3)    New->basePrice = fscan;
            else if(i==2)   New->comboPrice = fscan;
            else    New->upgradePrice = fscan;
            //printf("NO");
        }

        else{
            fscanf(fp, "%i", &iscan);
            fgetc(fp);
            if(i==6)    New->hierarchy = iscan;
            else if (i==7)  New->numberOfInitialInventory = iscan;
            else    New->numberOfPresentInventory = iscan;  
            printf("NO");
        }


    }
    if((*head)->next == NULL){
        (*head)->next = New;
        temp = New;
    }
    else{
        temp->next = New;
        temp = New;
    }


}



fclose(fp);

Here's the FOOD structure:

typedef struct foodtag{
char category[256];
char itemDescription[256];
char itemUnit[256];
int hierarchy;
int numberOfInitialInventory;
int numberOfPresentInventory;
float basePrice;
float comboPrice;
float upgradePrice;
struct foodtag *prev;
struct foodtag *next;
}FOOD;
halfer
  • 19,824
  • 17
  • 99
  • 186
  • You should post , at least `FOOD` struct. Best to have [MCVE](http://stackoverflow.com/help/mcve) – LPs Dec 06 '16 at 15:09
  • what is FOOD structure? – Sumit Gemini Dec 06 '16 at 15:09
  • If you get a segmentation fault, you should have a core file (depending on ulimit) to inspect. If not, you can just run the process under your debugger and it will stop at the segfault. – Useless Dec 06 '16 at 15:10
  • Hint: dont put the `if ()` and the condifional instruction on the same line, it's difficult to read and to debug. – Jabberwocky Dec 06 '16 at 15:11
  • `char *category;` is a pointer and is not allocated by your code. So `strcpy(New->category, scan);` ====> **booooooom** – LPs Dec 06 '16 at 15:14
  • Take also a look at [why-is-while-feof-file-always-wrong](http://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) – LPs Dec 06 '16 at 15:25
  • In addition to LPs comment: maybe you just want `char category[256];` in your `FOOD` structure. – Jabberwocky Dec 06 '16 at 15:26
  • 1
    Your debugger will tell you where exactly the crash happens. It's time to learn how to use a debugger now. – Jabberwocky Dec 06 '16 at 15:27
  • I don't see anything obviously wrong in this code apart from the `char *category` stuff you claim you have fixed in the meantime. Could you show us the textfile you read? If the file is too long, just show the first 10-15 lines. – Jabberwocky Dec 06 '16 at 15:30
  • ... and please show a [mcve] (yes we mean it). How is `fileName` declared ? – Jabberwocky Dec 06 '16 at 15:32
  • thank you @LPs. It turned out that feof is the problem – Patricia SONE Dec 06 '16 at 15:33

2 Answers2

1

(Posted on behalf of the OP).

I've replaced the while(!feof(fp)) with other condition and poofed it worked.

halfer
  • 19,824
  • 17
  • 99
  • 186
0

You are not allocating memory for category pointer of FOOD struct.

FOOD *my_food = malloc(sizeof(FOOD));
if (my_food != NULL)
{
    my_food->category = malloc(256);
    if (my_food->category != NULL)
    {
        // USAGE
    }
}

Edit form my comment to the question:

Take a look at why is while feof file always wrong.

Community
  • 1
  • 1
LPs
  • 16,045
  • 8
  • 30
  • 61