Saving structure to a file
Hi all,
I have a very simple array of structures. Structure is as follows
Code:
struct StockItem
{
int code;
char desc[20];
float price;
};
When this populate my items using this data structures, I need to save these items to disk.
Also, once these items have been saved to disk, I need to reload them back into my program the next time I start it up.
Can anyone help me with this please?
I have made an attempt, but am still pretty new to programming and can't get it to work and I think I am going down the wrong path.
Code as follows:
Code:
void save_data(StockItem save)
{
fstream fp;
fp.open("items.txt", ios::out | ios::app);
if (!fp)
{
cout << "\n*** Error opening file ***\n";
//exit(0);
}
fp << save.code;
for(unsigned short i = 0; i < 20; i++)
{
fp.put(save.desc[i]);
}
fp << save.price;
fp.close();
}
void print_data(void)
{
fstream fp;
fp.open("items.txt", ios::in);
if (!fp)
{
cout << "\n*** Error opening file ***\n";
//exit(0);
}
cout << fp;
fp.close();
}
All help would be greatly appricieted.
Thanks