0

`

void DisplayFood(int*NumOfFood,int*Rno,char *Names[],int*Price,int*Quantity,char*mfg[],char*exp[]) {
    int i =0;
    for(i=0;i<*NumOfFood;i++) {
        printf("\n%-20d",Rno[i]);
        printf("%-20s\t",Names[i]);
        printf("%-20d\t\t",Price[i]);
        printf("%-20s\t\t",mfg[i]);
        printf("%-10s\t",exp[i]);
        printf("%-20d\n",Quantity[i]);
        
    }

}

void ReadFile(int*NumOfFood,int*Rno,char*Names[],int*Price,int*Quantity,char*mfg[],char*exp[]) {
        char getexp[20],getmfg[20], getname[20];
        int rno,price,quantity;
            Names[*NumOfFood] = malloc(strlen(getname) + 1);
            mfg[*NumOfFood] = malloc(strlen(getmfg) + 1);
            exp[*NumOfFood] = malloc(strlen(getexp) + 1);
        int i = 0;
        FILE* fp;
        fp = fopen("MyFood1.txt", "r");
        if(fp) {
            for(;;) {
                fscanf(fp, "%10d %20[^\n] %20d %20d %20[^\n] %10[^\n]\n",&rno,getname,&price,&quantity,getmfg,getexp);
                Names[*NumOfFood] = malloc(strlen(getname) + 1);
                mfg[*NumOfFood] = malloc(strlen(getmfg) + 1);
                exp[*NumOfFood] = malloc(strlen(getexp) + 1);
                Rno[*NumOfFood] = rno;
                Price[*NumOfFood] = price;
                Quantity[*NumOfFood] = quantity;
                strcpy(Names[*NumOfFood],getname);
                strcpy(mfg[*NumOfFood],getmfg);
                strcpy(exp[*NumOfFood],getexp);

                (*NumOfFood)++;
                /* Read until end of file */
                if(feof(fp)) {
                    break;
                }   
            }
        }
        fclose(fp);
    }

int main() {
    
/* Declare for count element, dynamic allocate */
    int NumberOfFood=0,i;

/* Declare dynamic array */
    int*Rno = (int*)calloc(NumberOfFood,sizeof(int));
    int*Price = (int*)calloc(NumberOfFood,sizeof(int));
    int*Quantity = (int*)calloc(NumberOfFood,sizeof(int));
    
/* Declare array of char pointer */
    char **Names = (char**)calloc(NumberOfFood,sizeof(char*));
    char **MFG = (char**)calloc(NumberOfFood,sizeof(char*));
    char **EXP = (char**)calloc(NumberOfFood,sizeof(char*));
    

/* Get value from file and store into each array */

    ReadFile(&NumberOfFood,Rno,Names,Price,Quantity,MFG,EXP);
    DisplayFood(&NumberOfFood,Rno,Names,Price,Quantity,MFG,EXP);
*Names[],int*Price,int*Quantity,char*mfg[],char*exp[])

}

`

I have 6 dynamic allocate array and was provided data from "MyFood1.txt". I'm trying to show menu with 10 food with displayfood function, when i use printf("%-20s\t",Names[1]) in for...loop it's ok but when i instead 1 by i like: printf("%-20s\t",Names[i]) it work like this ;)))

enter image description here

  • 3
    `NumberOfFood` is 0 when you allocate the arrays. – stark Nov 03 '22 at 14:56
  • 1
    You're breaching your arrays *and* not allocating appropriate memory. Ex: How much memory do you think is allocated with `calloc(NumberOfFood,sizeof(char*));` when `NumberOfFood` is zero ? That allocation isn't going to magically resize itself later has `NumberOfFood` changes. In fact, *none* of your allocations in `main` are actually allocating any space whatsoever. Things like the initial `Names[*NumOfFood] = malloc(strlen(getname) + 1);` are also nonsense. `getname` is an indeterminate buffer with no string contained (yet). Therefore `strlen` invokes *undefined behavior*. – WhozCraig Nov 03 '22 at 14:59
  • Please don't use screenshots of text. Instead show code as well as input and output as formatted text in your question. – Gerhardh Nov 03 '22 at 15:28
  • `/* Read until end of file */ if(feof(fp)) { break; }` isn't the right way to use `feof`, which does *not* tell you reached end of file. If anywhere, it should be right after the `fscanf` line, but better (even essential) is instead to check the value retured by `fscanf` and use that to control the loop. As in `while(fscanf(...) == 6) { ... }`. – Weather Vane Nov 03 '22 at 15:46

0 Answers0