Thread: I need help with saving to a file PLEASE......... ::sobs:: please I can't take it

  1. #1
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427

    I need help with saving to a file PLEASE......... ::sobs:: please I can't take it

    Ok scenario one, Let's say I want to save the settings of a specific player to a file......how would I save it so that when I open it up again, open the file later on, all the settings will match to those of the actual game....for example......


    Player 1.....we save the data...
    health:100
    height:200

    ...........

    ok whatever let's say that's saved to a text file or something.....but then I want to open it so that the health of player 1 will correspond to that of the game......to health on the game.....


    please just show me a simple code and explain a little bit.....PLEASE!!!!!!!!!!!!!!!!!!!
    There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!

    "...The only real game I thank in the world is baseball..." --Babe Ruth

    "Life is beautiful"-Don Corleone right before he died.

    "The expert on anything was once a beginner" -Baseball poster I own.


    Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.

  2. #2
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    Simple example -

    Code:
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    class Player
    {
    private:
    	int health;
    	int height;
    public:
    	Player(int health_=0,int height_=0):health(health_),height(height_){}
    	
    	friend ostream& operator <<(ostream& os, const Player& p){
    		return os << p.health << ' ' << p.height;
    	}
    
    	friend istream& operator >>(istream& is,Player& p){
    		return is >> p.health >> p.height;
    	}
    };
    
    int main()
    {
    	//Create default player with no health or height
    	Player One;
    
    	//Get heath & height from cin
    	cin >> One;
    	
    	while (cin.fail()){
    		cin.clear();
    		cin.ignore(80,'\n');
    		cin >> One;
    	}
    
        //Open file for writing
    
    	ofstream os("player.dat");
    
    	if(!os)
    	{
    		cerr << "Error Opening File For Writing\n";
    		return 1;
    	}
    
    	os << One;
    
    	os.close();
    
    	//Open file for reading
    
    	ifstream is("player.dat");
    
    	if(!is)
    	{
    		cerr << "Error Opening File For Reading\n";
    		return 1;
    	}
    
    	is >> One;
    
    	if(is.fail())
    	{
    		cerr << "Error Reading File\n";
    		return 1;
    	}
    
    	//Output health/height to cout
    
    	cout << One;
    	
    	return 0;
    }

  3. #3
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427
    ok I understand your example, but my how come if this is in fact written to a file this doesn't make a file and fills it up with the info, because if this does make this into a file and stores the info how come there's no file with the info?
    There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!

    "...The only real game I thank in the world is baseball..." --Babe Ruth

    "Life is beautiful"-Don Corleone right before he died.

    "The expert on anything was once a beginner" -Baseball poster I own.


    Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.

  4. #4
    Linguistic Engineer... doubleanti's Avatar
    Join Date
    Aug 2001
    Location
    CA
    Posts
    2,459
    when you open the file, you check for creating upon nonexistance. your file io procedures should have flags or parameters for doing so...

  5. #5
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427
    THANK YOU PEOPLE


    here it goes what I did.

    Code:
    #include <iostream>
    #include <fstream.h>
    
    
    
    
    class player
    {
    public:
    player():
    health (10),
    speed (50){}
    friend ostream & operator <<(ostream& pl,  player&p)
    {
    return pl<<p.health<<" " <<p.speed;
    }
    friend istream& operator >>(istream& stats,  player&p)
    {
    return stats>>p.health>>p.speed;
    }
    
    
    
    void SetHealth(int health){health=health;}
    int const GetHealth() {return health;}
    private:
    int health;
    int speed;
    };
    
    int main()
    {
    
    player first;
    
    
    cin>>first;
    
    ofstream saveplayer ("playerstats.txt", ios::app);
    
    saveplayer<<first;
    saveplayer.close();
    
    ifstream read ("playerstats.txt");
    
    cout<<first;
    read.close();
    
    int x;
    cin>>x;
    
    
          return 0;
    }

    I'll comment it for my own sake tomorrow...........Thank you.
    There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!

    "...The only real game I thank in the world is baseball..." --Babe Ruth

    "Life is beautiful"-Don Corleone right before he died.

    "The expert on anything was once a beginner" -Baseball poster I own.


    Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.

  6. #6
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427
    Ok this works, but if I want to load the settings of a player without having first made it (new player) ........I can't...... like this game does it........http://cprogramming.com/cboard/showt...4&pagenumber=1 I want to do that, that you can save it and or retrieve it at a later time.............
    There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!

    "...The only real game I thank in the world is baseball..." --Babe Ruth

    "Life is beautiful"-Don Corleone right before he died.

    "The expert on anything was once a beginner" -Baseball poster I own.


    Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.

  7. #7
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427
    I mean if you've already made it previously and you quit the game and then you start up the game and want to load up the settings I can't do it........................
    There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!

    "...The only real game I thank in the world is baseball..." --Babe Ruth

    "Life is beautiful"-Don Corleone right before he died.

    "The expert on anything was once a beginner" -Baseball poster I own.


    Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.

  8. #8
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427
    this seems to be doing the trick for me if something is wrong on my comments please correct

    Code:
    #include <iostream>
    #include <fstream.h>
    
    
    
    
    class player
    {
    public:
    	player(){}//:
    //health (10),
    //speed (50){}
    
    
    //the overloading of this operator seems to be useless on this program.
    friend ostream & operator <<(ostream& pl,  player&p)
    {
    return pl<<p.health<<" " <<p.speed;
    }
    
    
    friend istream& operator >>(istream& stats,  player&p)
    {
    return stats>>p.health>>p.speed;
    //this here is overloaded so you can enter both a health and a speed
    //for the player for example when the computer asks you for an input
    //cin>>first.........then you input 10 (space) 50
    }
    
    
    
    void SetHealth(int health){health=health;}
    int const GetHealth() {return health;}
    
    int health;
    int speed;
    
    };
    
    int main()
    {
    
    player first;//looks like I would have to create an object
    //if I wanted to load the stats for a certain player I 
    //would have to create a player object.........
    
    
    /*cin>>first;
    
    ofstream saveplayer ("playerstats.txt", ios::app);
    
    saveplayer<<first.health<<"\n"<<endl;
    saveplayer<<first.speed<<"\n"<<endl;
    saveplayer.close();*/
    
    ifstream read ("playerstats.txt");
    
    read>>first.health;//boots up the stats into memory so we can boot it up to the class 
    //as use it
    read>>first.speed;
    cout<<first.health;//displays it on the screen
    cout<<first.speed;
    
    
    //cout<<first;//this doesn't work even though I overloaded this (<<) operator
    //I can't seem to get the specs for the whole class.
    read.close();
    
    int x;
    cin>>x;
    
    
          return 0;
    }
    There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!

    "...The only real game I thank in the world is baseball..." --Babe Ruth

    "Life is beautiful"-Don Corleone right before he died.

    "The expert on anything was once a beginner" -Baseball poster I own.


    Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.

  9. #9
    Registered User
    Join Date
    Jan 2002
    Posts
    387
    iostream.h is included in fstream.h, so there is no need to include iostream.h in your file

  10. #10
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Classes are a useful tool to represent items you want to manipulate, but from a final result standpoint there is nothing you can do with classes you can't do without (that I can think of). If you wish to work on the version of your program using classes I would suggest you drop the overloaded >> operator and use set the member variables one at a time. Also, I would advise against sending in an argument with the same name as a member (see the change in the line {health = health;} below. I have updated your program to show how it could be done.

    Code:
    #include <fstream.h>
    
    class player
    {
       public:
          friend ostream & operator <<(ostream& pl,  player&p);
    
          void SetHealth(int Health){health=Health;}
          void SetSpeed(int Speed){speed = Speed;}
          int const GetHealth() {return health;}
          int const GetHealth() {return speed;}
    
       private:
          int health;
          int speed;
    };
    
    ostream & operator <<(ostream& pl,  player&p)
    {
       return pl << p.health << " " << p.speed;
    }
    
    
    int main()
    {
    
      //declare a player
      player first;
      int dummy;
    
      //enter values for the first
      cout << "enter vaule for health" << endl;
      cin >> dummy;
      first.SetHealth(dummy);
    
      cout << "enter value for speed" << endl;
      cin >> dummy;
      first.SetSpeed(dummy);
    
      //demonstrate that first is ready to go
      cout << first << endl;
    
      ofstream saveplayer ("playerstats.txt");
    
      //write first to file
      saveplayer << first;
      saveplayer.close();
    
      //declare a different player to prove you are reading from file
      player second;
    
    
      ifstream read ("playerstats.txt");
    
      //read data from file into second
      read >> dummy;
      second.SetHealth(dummy);
      read >> dummy;
      second.SetSpeed(dummy);
    
      //prove second successfully loaded and ready to go.
      cout << second;
      read.close();
    
      //pause program to keep on screen to visualize output
      int x;
      cin>>x;
    
      return 0;
    }
    This represents the very basics of classes/file usage. You will undoubtedly want to get more sophisticated as you go along. May the next level would be to rewrite the drive program using a menu, to register new players, allow a player to log in, or allow logged in player to play the game. After each game update the data for the given player if applicable. Depending on type of game, allow player to player repeated games until the log out. The player list and stats would be stored in a playerStat file. You can add and delete players as they desire. To log in the player must enter an identifier to match something in the player list. The player list will be read from file into a container in the program so it can be searched for the given player input id. If it finds a match then load player stats stored in player list into game, if appropriate. etc. etc.
    Last edited by elad; 03-25-2002 at 03:44 PM.

  11. #11
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427
    Any ideas why the overloading of this operator doesn't seem to be working for me?


    or is it overloaded correctly?



    //the overloading of this operator seems to be useless on this program.
    friend ostream & operator <<(ostream& pl, player&p)
    {
    return pl<<p.health<<" " <<p.speed;
    }
    There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!

    "...The only real game I thank in the world is baseball..." --Babe Ruth

    "Life is beautiful"-Don Corleone right before he died.

    "The expert on anything was once a beginner" -Baseball poster I own.


    Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.

  12. #12
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    >but my how come if this is in fact written to a file this doesn't make a file and fills it up with the info, because if this does make this into a file and stores the info how come there's no file with the info?<

    I'm having trouble extracting the point from this sentence, but it's probably due to you using pre-standard header files -

    <fstream.h>

    is included with compilers to maintain backward compatibility, but may not always be.

    The standard headers

    <fstream>

    changed some of the behaviour of i/o streams. If you want to write standard code with future portability in mind, these are the headers you should use.

  13. #13
    Registered User
    Join Date
    Dec 2001
    Posts
    421
    here it goes what I did.

    HAHAH!!! you're kidding me aren't ya!!!!

    I'll tell you what you did, you copied and pasted someone else's code! Sorensen did the work for you.
    Quidquid latine dictum sit, altum sonatur.
    Whatever is said in Latin sounds profound.

  14. #14
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427
    Originally posted by Uraldor
    here it goes what I did.

    HAHAH!!! you're kidding me aren't ya!!!!

    I'll tell you what you did, you copied and pasted someone else's code! Sorensen did the work for you.

    I AM MORE CALM DOWN.......HUGE EDIT........

    OK I am more tranquil now, please mister URALDOR if you have nothing that is going to contribute to this thread plese refrain yourself from posting here which is only going to get in the way of my learning routine/environment, I had some really nasty things to say to you. In fact I did so I edited it, I am better than that, please if you have nothing constructive, just please stay from this and any othere threads I start. For you information after I saw that code that was give to me, I did some research an made some small changes to it, and tried it out, to make sure I got it.
    Last edited by incognito; 03-25-2002 at 04:43 PM.
    There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!

    "...The only real game I thank in the world is baseball..." --Babe Ruth

    "Life is beautiful"-Don Corleone right before he died.

    "The expert on anything was once a beginner" -Baseball poster I own.


    Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.

  15. #15
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427
    Originally posted by Sorensen
    >but my how come if this is in fact written to a file this doesn't make a file and fills it up with the info, because if this does make this into a file and stores the info how come there's no file with the info?<

    I'm having trouble extracting the point from this sentence, but it's probably due to you using pre-standard header files -

    <fstream.h>

    is included with compilers to maintain backward compatibility, but may not always be.

    The standard headers

    <fstream>

    changed some of the behaviour of i/o streams. If you want to write standard code with future portability in mind, these are the headers you should use.

    Yeah thanks I got it seems that the compiler I am using when I run it from within the compiler it doesn't work, but when I got to the directory and run the exe from there it works Thank....... and sorry about my mood earlier, he just really ........ed me off.
    There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!

    "...The only real game I thank in the world is baseball..." --Babe Ruth

    "Life is beautiful"-Don Corleone right before he died.

    "The expert on anything was once a beginner" -Baseball poster I own.


    Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  2. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 07:54 AM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM