Yet another C++ problem...

Error message

Deprecated function: implode(): Passing glue string after array is deprecated. Swap the parameters in drupal_get_feeds() (line 394 of /var/www/pied-piper.ermarian.net/includes/common.inc).
AuthorTopic: Yet another C++ problem...
Master
Member # 4614
Profile Homepage #0
Since I'm kind of stuck again. :/

This time it has to do with reading and writing classes to and from files. Here's what I have:

void writefile()
{
TeamList TL;
char filename[20];
cout<<"Enter filename: ";
cin.ignore(1, '\n');
cin.getline(filename, 20);
ofstream fout(filename);
fout.write((char*) &TL, sizeof TL);
fout.close();
}

void readfile()
{
char filename[20];
cout<<"Enter filename: ";
cin.ignore(1, '\n');
cin.getline(filename, 20);
ifstream fin(filename);
if(!fin){cout<<"No such file.";goto end;}
fin.read((char*) &TL, sizeof TL);

int i;
cout<<"\n File Menu : "<<filename<<"\n";
cout<<" -----------\n";
cout<<" (1) Create New Team\n";
cout<<" (2) View/Edit Teams\n";
cout<<" (3) Play Game (requires 2 teams)\n";
cout<<" (4) Save Teams\n";
cout<<" (5) Load New File\n";
cout<<" (6) Quit\n\n\n";
fin.close();
end:;
}
TeamList is a linked list of teams I created as a class that contains a bunch of other stuff, but I doubt this has anything to do with the problem at hand.

What I'm trying to do is be able to save a TeamList to a file and then be able to reopen it again at a later time. writefile() creates a file with a user-specified name. readfile() reads the file and displays a menu of what to do with that file.

writefile() seems to work fine; it creates a file and everything, but when I try to read the file, it gives me a whole bunch of irrevelant characters and then crashes. The menu is not displayed.

Anyone know what I could have done wrong?

--------------------
-ben4808

For those who love to spam:
CSM Forums
RIFQ
Posts: 3360 | Registered: Friday, June 25 2004 07:00
Post Navel Trauma ^_^
Member # 67
Profile Homepage #1
The problem is that a linked list is a load of pointers, and you can't sensibly write pointers to a file like that. You have to separately write each member of the linked list, and your reading routine then has to take them and join them up.

--------------------
Barcoorah: I even did it to a big dorset ram.

desperance.net - Don't follow this link
Posts: 1798 | Registered: Thursday, October 4 2001 07:00
Master
Member # 4614
Profile Homepage #2
Actually, the TeamList class contains another linked list which stores all the players on that team, so...

You're saying that doing what I want to do would be very complicated.

--------------------
-ben4808

For those who love to spam:
CSM Forums
RIFQ
Posts: 3360 | Registered: Friday, June 25 2004 07:00
Post Navel Trauma ^_^
Member # 67
Profile Homepage #3
Basically, yes.

--------------------
Barcoorah: I even did it to a big dorset ram.

desperance.net - Don't follow this link
Posts: 1798 | Registered: Thursday, October 4 2001 07:00