Need help guys! my problem is i cannot store file data into an array of structures. I can already read the file data and also tokenized it so as to separate data. here is content of the file that i am reading:
200001, hamburger, 30.50
100000, cheeseburger, 21.00
the name of the file is inventory.ini
i can already read the file with this output:
200001
hamburger
30.50
100000
cheeseburger
21.00
i used the strtok() function.
the problem is i cannot store these data in a structure! here is my code:
header file is saved separately but IN THE SAME PATH as to where my code is saved:
typedef struct productDescriptionType
{
char inventory_code[7];
char item_description[20];
float unit_price;
} INVENTORY;
this is the inventoryStruct.h
this is the main program
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include "inventoryStruct.h"
struct productDescriptionType* product; // inventory array
void printInfo(INVENTORY product) //just to check if i successfully transferred the file data to the structure. apparently i still cant reach this part. :(
{
printf("%s",product.inventory_code);
printf("%s",product.item_description);
printf("%f",product.unit_price);
}
int main()
{
INVENTORY prodDesc; //declare struct
int ctr_file; //sample number of lines in the file
FILE *ptr; //pointer to open inventory.ini
ctr_file = 4; //sample line number
int i; //index for the inventory array
char tempStorage[30]; //temporary storage of the tokenized data
char * tokenized; //tokenized data value is stored in this pointer
product = (struct productDescriptionType*) malloc(ctr_file * sizeof(struct productDescriptionType)); // allocate memory for the structure
ptr = fopen("inventory.ini","r"); //read .ini file
if ( ptr==NULL )
{
perror("Error...");
system("pause");
exit(1);
}
for(i = 0;i<ctr_file;i++)
{
while (!feof(ptr))
{
tokenized = strtok(tempStorage,", ");
scanf("%s",prodDesc[i].inventory_code); //this is the error! and the next 2 lines!
scanf("%s",prodDesc[i].item_description);
scanf("%f",prodDesc[i].unit_price);
}
}
for(i = 0;i<ctr_file;i++) // program can't push through in this part
{
printfInfo(prodDesc[i]);
}
fclose(ptr);
system("pause");
}
please help!.my question again is how to store a file data into an array of structures!thanks!