I need to do some file reading and writing for a little project I''m working on, but I''m not exactly sure how to accomplish what I want.
File Writing:
Saves the
CPlayer and
CWeapon classes to the file
Players.dat in binary. I''m using binary to keep the players from altering their stats with Notepad.
File Reading:
Reads the contents of the saved
Players.dat file and allows the user to load their previously saved player.
Now, here''s the basic setup of what I want to happen:
The player is prompted at the beginning asking whether they want a new player or would like to load a saved one. If they want a saved player, the program will read the contents of
Players.dat and display the names of all the players they have saved. So, if in the past they created some new players named
Stonie and
Freddy, those names would be displayed on the screen allowing them to choose one of them. After choosing the one they want, the saved contents of that player inside
Players.dat would be loaded into the
CPlayer class. So, this is what I have so far, which is pretty much nothing. In fact, it''s so small it looks more like a prototype than a working function.
void CPlayer::LoadSavedPlayer(CPlayer * thePlayer, CWeapon * theWeapon)
{
ifstream loadPlayer("Players.dat", ios::in | ios::binary);
loadPlayer.read((char *)&thePlayer, sizeof(CPlayer));
loadPlayer.close();
}
void CPlayer::SavePlayer(CPlayer * thePlayer, CWeapon * theWeapon)
{
ofstream savePlayer("Players.dat", ios::out | ios::app | ios::binary);
savePlayer.write((char *)&thePlayer, sizeof(CPlayer));
savePlayer.close();
}
I am aware of the fact I''m not using
theWeapon in the above functions. I need to get
thePlayer working before I do that.
Here is one big problem I''m running in to.
The LoadSavedPlayer() function is called before the objects
thePlayer and
theWeapon have even been created. Therefore, pointers to those classes can''t be passed to the function. Here is a brief outline of the order the functions are called.
main(){
CPlayer * pP = PromptNewPlayer(); // asks for new or saved player
CWeapon * pW = Weapon(0); // creates a new, empty weapon
MenuMain(pP, pW); // here''s where real program begins
}
Inside of the function PromptNewPlayer(), the user has a choice of CreateNewPlayer() or LoadSavedPlayer(). But as you can see, the function LoadSavedPlayer() can''t be called without the class pointers, which goes back to my problem...they haven''t been created yet. Both of them are created only after the CWeapon constructor before
MenuMain().
Anyway, I''ve thrown a lot of crap out on the table, I know. But I wanted to get as much out as possible to cut down on the number of posts I''ll have to make. Thanks in advance.
---
Joker2000
Stevie Ray Vaughan - The Legend