reading/writing integers to a file

This is a discussion on reading/writing integers to a file within the C++ Programming forums, part of the General Programming Boards category; i want to make a high score list for my game, but i can't figure out how to read and ...

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    26

    reading/writing integers to a file

    i want to make a high score list for my game, but i can't figure out how to read and write integers from a file.

  2. #2
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802

  3. #3
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427
    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
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427
    Maybe not the best way, but it works for me
    Code:
    #include <iostream>
    #include <fstream.h>
    
    
    
    
    class player
    {
    public:
    
    player(){}//:
    //health (10),
    //speed (50){}
    
    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 (Enter) 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 a class
    //if I wanted to load the stats for a certain player I 
    //would have to create a player class.........
    
    cout<<"Enter the Player's Health and speed separated by pressing Enter"<<endl;
    cin>>first;
    
    ofstream saveplayer ("playerstats.txt", ios::app);
    
    saveplayer<<first.health<<"\n"<<endl;
    saveplayer<<first.speed<<"\n"<<endl;
    saveplayer.close();
    
    
    ifstream read ("playerstats.txt");
    cout<<"Now I shall display the stats"<<endl;
    
    read>>first.health;//boots up the stats.......kind of
    read>>first.speed;
    cout<<first.health<<endl;//displays it on the screen
    cout<<first.speed;
    
    read.close();
    
    int x;//this is when I used DevC++
    cin>>x;
    
    //this is also some old stuff I kind of reworked some time ago.
          return 0;
    }
    Last edited by incognito; 05-21-2002 at 05:48 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.

  5. #5
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427
    http://www.cplusplus.com/doc/tutorial/tut6-1.html


    well I hope that gets you started.
    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
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Try this.
    Code:
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    int main(void)
    {
       int scores[10] = {30000,29000,25000,20000,10000,5000,4000,3000,200,100};
       int i, score;
       char filename[80] = "scores.txt";
    
       //Write high score file
       ofstream out(filename);
       if (!out)
       {
          cout << "Unable to open file:" << filename << endl;
          return 1;
       }
    
       for (i=0; i<10; i++)
          out << scores[i] << " ";
       out << endl;
       out.close();
    
       //Read high score file
       ifstream in(filename);
       if (!out)
       {
          cout << "Unable to open file:" << filename << endl;
          return 1;
       }
       cout << "*****     Top Ten High Scores     *****" << endl;
       for (i=0; i<10; i++)
       {
          in >> score;
          cout.width(5);
          cout << i+1 << ": ";
          cout.width(10);
          cout << score << endl;
       }
       in.close();
    
       return 0;
    }

  7. #7
    Registered User
    Join Date
    Feb 2002
    Posts
    26
    thanks for the help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 01:44 PM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 01:36 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 11:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21