Saving / reading structures to / from disk

E

eth0

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.


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;
}
 
D

Deming He

eth0 said:
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.


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.

#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()
{
file://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:
{
file://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;
}

file://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;
}

Which two functions not working for you? What errors you got?
Why was StockItem stock[125]; commented out but later in main() you need to
use stock object(s)?
Where was 's "ctr" defined in main(), save_data(stock[ctr]);?
Note that your "save_data()" was declared with a parameter of type StockItem
*, not StockItem.
 
H

Heinz Ozwirk

: 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.

[...]

: //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();
: }

You are not saving one of your StockItem's (not to mention an array of them), but only its address in memory. Did you ever look how big (or small) the file is?

Heinz
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,016
Latest member
TatianaCha

Latest Threads

Top