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