Hi Davi, I understand what you mean. however this also gives the same output file as before. My main problem is getting the data back into the program after I have exited and restarted. This is what I have been stuck with for the past few days. I'm beginning to think its not possible (although I know it is) as nobody, including experienced coders on c++ forums seems to be able to get it to work. If you get a second, could you compile the code and run the program, you'll see what I mean. Thanks, Ged. On Tuesday 06 January 2004 19:32, davicastro@terra.com.br wrote:
The main error is at:
void save_data(StockItem * tosave) .... fout.write((char *) &tosave, sizeof(tosave));
sizeof(tosave) is the same of sizeof(StockItem *), and probably 4 bytes. Furthermore, you should not use the & before tosave, since it is already a pointer (array). The same mistake occurs in the load code. If you want to store a C array you should also store its size (and also pass it as argument to others functions). If you want to fix this code, you should read some more about C pointers and arrays until everything get clear. Then it will be straightforward.
[]s Davi
-=SuSE Linux 9.0=-
How much data is there? What about structures and pointers to structures perhaps a binary tree or doubly linked list? Such things are already done in PASCAL its only a matter of syntax. CWSIV On Tue, 2004-01-06 at 11:30, Ged wrote:
Hi Davi,
I understand what you mean. however this also gives the same output file as before. My main problem is getting the data back into the program after I have exited and restarted. This is what I have been stuck with for the past few days. I'm beginning to think its not possible (although I know it is) as nobody, including experienced coders on c++ forums seems to be able to get it to work.
If you get a second, could you compile the code and run the program, you'll see what I mean.
Thanks,
Ged.
On Tuesday 06 January 2004 19:32, davicastro@terra.com.br wrote:
The main error is at:
void save_data(StockItem * tosave) .... fout.write((char *) &tosave, sizeof(tosave));
sizeof(tosave) is the same of sizeof(StockItem *), and probably 4 bytes. Furthermore, you should not use the & before tosave, since it is already a pointer (array). The same mistake occurs in the load code. If you want to store a C array you should also store its size (and also pass it as argument to others functions). If you want to fix this code, you should read some more about C pointers and arrays until everything get clear. Then it will be straightforward.
[]s Davi
-=SuSE Linux 9.0=-
Tuesday, 6. January 2004 20:30 Ged wrote:
My main problem is getting the data back into the program after I have exited and restarted.
Hi Ged, the problem is that you write the whole arry into your file. So when you try to reload it, your programm wont't know how many items you entered before. Write only as many items to the file as you hve in your array. When you reading the items back from file you can count them until you reaching the end of file. With this value you can set the num_items variable. The code below is working. But it's not perfect. You should do some checks, eg. Is the file open? Went something wrong while reading/writing to the file? ... Something more. Maybe it's not a good idea to store your stock items in an array. Better is to use a list. The standart c++ lib has some ready to use templates. Maybe you should read some more about this in a good C++ book. Good luck Hartmut --------------------------------------------------- #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 writeToDisk( StockItem*, int ); int readFromDisk( StockItem* ); 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; 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: writeToDisk( stock, num_items ); // ^^^^^^^^^ <-- writing NOT the whole array // to disk. break; case 4: num_items = readFromDisk( stock ); // ^^^^^^^^^ <---- setting the number items we read from file break; default: break; } }while (ans!=5); return 0; system("PAUSE"); return 0; } void writeToDisk( StockItem* array, int numOfItems ) { ofstream outfile( "items.txt" ); for( int index = 0; index < numOfItems; index++ ) outfile.write( (char*)&array[index], sizeof(StockItem) ); } int readFromDisk( StockItem* array ) { int numOfItems = 0; ifstream infile( "items.txt" ); StockItem tmp_stockItem; // read from the file until we are reaching the eof while( infile.read( (char*)&tmp_stockItem, sizeof(tmp_stockItem) ) ) { // after a successfull read we storing the the item into the array memcpy( &array[numOfItems], &tmp_stockItem, sizeof( array[numOfItems] ) ); numOfItems++; } // returning the number of array items we read from the file return numOfItems; } 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 >> stock_item.desc; 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"; } } ---------------------------------------------
participants (3)
-
Carl William Spitzer IV
-
Ged
-
Hartmut.Huelsmann@t-online.de