Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jan 4th, 2006, 10:24 PM   #1
lamefif
Newbie
 
lamefif's Avatar
 
Join Date: Mar 2005
Posts: 16
Rep Power: 0 lamefif is on a distinguished road
saving and loading an array of object in acii file

Ok with your help guys im near the end .
This is my main class:
http://www3.unl.ac.uk:9102/lep043/include.htm
as you can see I have two arrays carray and sarray which are populated by objects, I don’t know what the best way of saving and load them. Or rather I haven’t got a clue how to do it.
Should I save the data of all objects into a string and then save the string, or is there a better way to save and load them.
Here is an example I got from this book im reading :
to speed up your memory search.
Is not helping me much because there saving as binary and I want text and somehow they end up saveing char.

Thanks
lamefif is offline   Reply With Quote
Old Jan 4th, 2006, 10:49 PM   #2
nindoja
Programmer
 
Join Date: Jun 2005
Posts: 92
Rep Power: 4 nindoja is on a distinguished road
Create an ofstream object, and open a file. Then print the data members of the objects on seperate lines. repeat for each object. When loading, create in ifstream object, and read each line into a data member of each object. Repeat until EOF is found.
nindoja is offline   Reply With Quote
Old Jan 5th, 2006, 3:08 PM   #3
lamefif
Newbie
 
lamefif's Avatar
 
Join Date: Mar 2005
Posts: 16
Rep Power: 0 lamefif is on a distinguished road
here is my attempt at loading a file:
dont laugh
if(action=="open"){
	ifstream imput("outfile.txt");
	for(int i=0;i<6;i++){
		string tempName="";
		float temppPrice=0,tempsPrice=0;
		int tempQ=0;
		imput >>tempName >> temppPrice >> tempsPrice>>tempQ;
		sarray[i].setName(tempName);
		sarray[i].setpPrice(temppPrice);
		sarray[i].setsPrice(tempsPrice);
		sarray[i].setQuantity(tempQ);
		}

	cout<<"done"<<endl;
		}
	}
file format:
Red Stuff
10.5
5
20.5
Green Stuff
10.5
5
20.5
Yellow Stuff
10.5
5
20.5
Orange Stuff
10.5
5
20.5
Pepper Stuff
10.5
5
20.5
Suger Stuff
10.5
5
20.5
but im getting this:
----------------------------------------------------------
Pruduct 1 Red | 0 | 0 | 0
----------------------------------------------------------
Pruduct 2 | 0 | 0 | 0
----------------------------------------------------------
Pruduct 3 | 0 | 0 | 0
----------------------------------------------------------
Pruduct 4 | 0 | 0 | 0
----------------------------------------------------------
Pruduct 5 | 0 | 0 | 0
----------------------------------------------------------
Pruduct 6 | 0 | 0 | 0
----------------------------------------------------------
instead of:
----------------------------------------------------------
Pruduct 1 Red Stuff | 10.5 | 20.5 | 5
----------------------------------------------------------
Pruduct 2 Green Stuff | 10.5 | 20.5 | 5
----------------------------------------------------------
Pruduct 3 Yellow Stuff | 10.5 | 20.5 | 5
----------------------------------------------------------
Pruduct 4 Orange Stuff | 10.5 | 20.5 | 5
----------------------------------------------------------
Pruduct 5 Pepper Stuff | 10.5 | 20.5 | 5
----------------------------------------------------------
Pruduct 6 Suger Stuff | 10.5 | 20.5 | 5
----------------------------------------------------------

thanks
lamefif is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 8:49 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC