![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Mar 2005
Posts: 16
Rep Power: 0
![]() |
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 |
|
|
|
|
|
#2 |
|
Programmer
Join Date: Jun 2005
Posts: 92
Rep Power: 4
![]() |
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.
|
|
|
|
|
|
#3 |
|
Newbie
Join Date: Mar 2005
Posts: 16
Rep Power: 0
![]() |
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;
}
}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 |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|