(C++) Saving / reading structures to / from disk
Hi all, I hate to ask like this as I normally like to work things out for myself, but I am running out of ideas....and hair !!! I am writing a stock inventory program using structures. The program data is initially populated by hand, and then there is an option to save the array of structures to disk. If the program is exited, the data will need to be inserted back into the program, hence the load function. My main problem is, I can't get these 2 functions to work. I have been working on this section for nearly 3 days now, and do not seem to be getting anywhere. I do not know any programmers to ask for help, so am relying on forums, but I have only been programming for about 2-3 months, so its difficult to implement what advise people give you. The program compiles, and I think the problem may lie in the way the data is being passed to the functions, but I am not experienced enough yet to solve it. Would anyone be able to get these functions working for me please. I have a lot more code to put into this program, but I can't move on until this part is complete. Like I said, I would not normally ask like this, but I am running out of options. Thanks to anyone who can help me, my code so far is 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); void save_data(StockItem * tosave); 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[ctr]); 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; 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"; } cout << "Press any key to return to the menu"; if(cin >> ret) return; } //Problem sections void save_data(StockItem * tosave) { ofstream fout("items.txt", ios::binary); if(!fout) { cout << "\n*** Error opening file ***\n"; } fout.write((char *) &tosave, sizeof(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; } ----------------------------------------- Email provided by http://www.ntlhome.com/
participants (1)
-
ged.suse@ntlworld.com