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 #include #include #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
Your first issue (why the program won't compile) is you're treating the variable "prodDesc" as an array and it has not been declared as such.
I can't see anywhere in your code (unless I'm going blind) where you are actually reading the file. You use the scanf() function which takes it's input from stdin.
You're using tempStorage in your strtok call, but how is this field being set in the first place?
You have many other errors, but I think it's best that you sort out exactly what you want to do with this program first and learn how to do things like declare an array of structures, read a file, tokenise a string and assign data to structure members.
You need to:
- read a record from the input file (perhaps using the fgets function) - tokenise the string obtained via the fgets call - iterate through the tokens and assign each token to a member in your structure. - repeat until EOF
Whilst developing the program, take small bites. Don't try and write the whole program in one hit. Make sure you are opening/reading the file correctly first. Then learn how to use strtok correctly (a Google will help out). Then learn how to assign data to the member of structure. In each iteration, make sure you have a working program before moving on to writing the next "feature" of the program.
One final thing - when posting code, use code tags.
Cheers JD