Hi The writing to the file is working, but the load is still need to be done. If I will have the necessary time to do it I will, but that is not a promise :) Maybe this helps you ... Zsolt #include <iostream> #include <fstream> #include <string.h> using namespace std; struct StockItem { int code; char desc[20]; float price; } stock[125]; /*struct bill_line { StockItem product; float weight; float cost; }; struct bill { bill_line items[20]; int num_lines; };*/ void disp_menu(void); struct StockItem enter_data(); void see_stock(StockItem stock[125], int num_items); void save_data(StockItem * tosave, int size); StockItem *load_data(StockItem toload[]); int main() { //StockItem stock[125]; int ans; int num_items = 0; do { do { disp_menu(); cin >> ans; } while((ans < 1) || (ans > 6)); switch (ans) { case 1: stock[num_items] = enter_data(); num_items++; break; case 2: see_stock(stock, num_items); break; case 3: save_data(stock, num_items); break; case 4: { //StockItem temp[125]; StockItem *eerrmm = load_data(stock); break; } default: break; } } while(ans != 5); return 0; } void disp_menu() { cout << "\n*** The Corner Shop ***\n\n"; cout << "Select an option: \n\n"; cout << "\t1. Enter new stock " << endl; cout << "\t2. See current stock " << endl; cout << "\t3. Save stock to disk " << endl; cout << "\t4. Load stock from disk " << endl; cout << "\t5. Exit \n" << endl; cout << "option> "; } StockItem enter_data() { StockItem stock_item; memset(stock_item.desc, 0, sizeof(stock_item.desc)); cout << "\n\nWhat is the product code: "; cin >> stock_item.code; cout << "What is the product description: "; cin.ignore(); cin.getline(stock_item.desc, 20); cout << "What is the product price: "; cin >> stock_item.price; return (stock_item); } void see_stock(StockItem stock[125], int num_items) { int ctr; char ret; cout << "\n\nHere is the stock listing:\n\n"; for(ctr = 0; ctr < num_items; ++ctr) { cout << "Item " << ctr + 1 << endl; cout << "Product Code: " << stock[ctr].code << endl; cout << "Product Description: " << stock[ctr].desc << endl; cout << "Price: " << stock[ctr].price << endl; cout << "------------------------------------------------\n"; } cout << "Press any key to return to the menu"; if(cin >> ret) return; } //Problem sections void save_data(StockItem* tosave, int size) { ofstream fout("items.txt", ios::binary); if(!fout) { cout << "\n*** Error opening file ***\n"; } for(int i = 0; i < size; i++){ fout<<tosave->code<<":"<<tosave->desc<<":"<<tosave->price<<":"; tosave++; } fout.close(); } StockItem *load_data(StockItem * toload) { ifstream fin("items.txt", ios::binary); if(!fin) { cout << "\n*** Error opening file ***\n"; } fin.read((char *) &toload, sizeof(toload)); fin.close(); cout << toload; return toload; } ged.suse@ntlworld.com wrote:
Thanks to all who have helped so far :)
I don't have much time left to get this program running, so I have opted to drop the functions and try and get it running in the main()..... ....however, I still can't get it to do what I want.
Can anyone PLEASE help me with this. I would be very grateful, I need to get this program running.
Cade now as follows:
#include <iostream> #include <fstream>
using namespace std;
struct StockItem { int code; char desc[20]; float price; }stock[125];
/*struct bill_line { StockItem product; float weight; float cost; };
struct bill { bill_line items[20]; int num_lines; };*/
void disp_menu(void); struct StockItem enter_data(); void see_stock(StockItem stock[125], int num_items);
main() { int ans, ctr; int num_items = 0; FILE *item_file = NULL;
do { do { disp_menu(); cin >> ans; } while ((ans<1) || (ans>6));
switch (ans) { case 1: stock[num_items] = enter_data(); num_items++; break; case 2: see_stock(stock, num_items); break; case 3: { item_file = fopen("items.txt", "ab"); for (ctr=0;ctr<125;++ctr) { fwrite(&stock[ctr], sizeof(StockItem), 3, item_file); } fclose(item_file); break; } case 4: { item_file = fopen("items.txt", "rb"); for (ctr=0;ctr<125;++ctr) { fread(&stock[ctr], sizeof(StockItem), 3, item_file); } fclose(item_file); break; } default: break; } }while (ans!=5);
return 0;
system("PAUSE"); return 0; }
void disp_menu() { cout << "\n*** The Corner Shop ***\n\n"; cout << "Select an option: \n\n"; cout << "\t1. Enter new stock " << endl; cout << "\t2. See current stock " << endl; cout << "\t3. Save stock to disk " << endl; cout << "\t4. Load stock from disk " << endl; cout << "\t5. Exit \n" << endl; cout << "option> "; }
StockItem enter_data() { StockItem stock_item;
cout << "\n\nWhat is the product code: "; cin >> stock_item.code; cout << "What is the product description: "; fflush(stdin); cin.getline(stock_item.desc, 20); cout << "What is the product price: "; cin >> stock_item.price;
return (stock_item); }
void see_stock(StockItem stock[125], int num_items) { int ctr; char ret;
cout << "\n\nHere is the stock listing:\n\n"; for (ctr = 0; ctr < num_items; ++ctr) { cout << "Item " << ctr+1 << endl; cout << "Product Code: " << stock[ctr].code << endl; cout << "Product Description: " << stock[ctr].desc << endl; cout << "Price: " << stock[ctr].price << endl; cout << "------------------------------------------------\n"; } }
From: Salman Khilji <skhilji@tampabay.rr.com> Date: 2004/01/06 Tue AM 06:32:27 GMT To: ged.suse@ntlworld.com Subject: Re: [suse-programming-e] (C++) Saving / reading structures to / from disk
Sorry I don't have time to give you a complete implementation, but lets see what I can pull out of my brain. the following won't compile, but will give you a brief idea as how to read and write a structure to/from disk. The following will NOT work with structure that have embedded pointers in them.
example:
struct Foo { float bar; int john; };
Foo array[2];
... populate the array of structs...
FILE * file = fopen("myfile", "w+b"); fwrite(file, array, sizeof(array)); // I know the arguments are wrong here, but lookup the docs to correct it.
To read it back, first use fopen call and read it into a char array.
Then do this.
Foo * array;
array = (Foo*) myCharArray;
After this, you should be able to see what the contents of Foo are.
I have an example that does this, but its at my office and I will have to dig it out.
Salman
----------------------------------------- Email provided by http://www.ntlhome.com/