473,378 Members | 1,390 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Saving / reading structures to / from disk

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;
}
Jul 22 '05 #1
2 2274
eth0 <et*********@yahoo.co.uk> wrote in message
news:8d**************************@posting.google.c om...
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.
Jul 22 '05 #2

"eth0" <et*********@yahoo.co.uk> schrieb im Newsbeitrag news:8d**************************@posting.google.c om...
: 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
Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

138
by: theodp | last post by:
--> From http://www.techdirt.com/articles/20040406/1349225.shtml Microsoft Patents Saving The Name Of A Game Contributed by Mike on Tuesday, April 6th, 2004 @ 01:49PM from the...
4
by: dale zhang | last post by:
Hi, I am trying to save and read an image from MS Access DB based on the following article: http://www.vbdotnetheaven.com/Code/Sept2003/2175.asp Right now, I saved images without any...
7
by: John Dann | last post by:
I'm trying to read some binary data from a file created by another program. I know the binary file format but can't change or control the format. The binary data is organised such that it should...
2
by: Mark Denardo | last post by:
Hi, I need some expert GDI+ person to help me with my RoundOffImage Function: What I'm trying to do is take in an image, crop off the edges around an ellipse region I set up, and then return the...
4
by: Pedro Leite | last post by:
Good Afternoon. the code below is properly retreiving binary data from a database and saving it. but instead of saving at client machine is saving at the server machine. what is wrong with my...
7
by: ConfusedAlot | last post by:
This is my code for a database, whenever i save the database to the file 'database.txt' and display the results i get this; name - (correct, is what i put in) pin - 3435973836 slew rate -...
10
by: =?Utf-8?B?TmFuZCBLaXNob3JlIEd1cHRh?= | last post by:
I have a binary file created using C++ that contains an object of the structure: struct student { int roll_no; char name; char qualification; };
1
by: Jonathan Wood | last post by:
In the past, using either C++ or VB (classic), I routinely stored records of data as an array of structures, and then simply read and wrote them to and from a file using basic file operations. ...
6
by: Karl | last post by:
Hi all, It may seem like a rather odd request (or not) but I would like to be able to create a file (doc, jpg, xls or one of many other files that can be automated) on a website and stream it to...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.