Saving game files

This is a discussion on Saving game files within the C++ Programming forums, part of the General Programming Boards category; How do you save and recall variables to a seperate file that the user can name?...

  1. #1
    His posts are far and few Esparno's Avatar
    Join Date
    Mar 2002
    Posts
    100

    Saving game files

    How do you save and recall variables to a seperate file that the user can name?
    Signature is optional, I didnt opt for one.

  2. #2
    I'm Back
    Join Date
    Dec 2001
    Posts
    556
    use file-handling

    ofstream to write, ifstream to read or fstream for both at the same time.


    Code:
    #include<iostream.h>
    #include<fstream.h>
    #incude<conio.h>
    int main()
    {
    ofstream fout;
    ifstream fin;
    
    int score;
    char fname[13];
    clrscr();
    
    cout<<"enter filename : ";
    cin.getline(fname,12);
    
    fout.open(fname);
    fout<<score;
    fout.close();
    
    cout<<"DATA STORED."
    getch();
    
    clrscr();
    cout<<"THE DATA STORED IN : "<<fname<<" IS :\n";
    fin.open(fname);
    fin>>score;
    cout<<score;
    fin.close();
    
    getch();
    return 0;
    }
    heres an instant code, not tested though

  3. #3
    His posts are far and few Esparno's Avatar
    Join Date
    Mar 2002
    Posts
    100
    Ok, im kind of a newby at C++ programming, can you put some comments in there so I know how to understand it and impliment it in this game im making, its a text-based RPG by the way. At least tell me where it is saving variables to the file and where it is recalling them. Thanks in advance.
    Signature is optional, I didnt opt for one.

  4. #4
    I'm Back
    Join Date
    Dec 2001
    Posts
    556
    Code:
    #include<iostream.h>
    #include<fstream.h> // for file i/o
    #incude<conio.h>
    int main()
    {
    ofstream fout; // ofstream opens file in output mode ie for writing
    ifstream fin; // opens for reading
    
    int score; // var that we'll store
    char fname[13]; //filename
    clrscr();
    
    cout<<"enter filename : ";
    cin.getline(fname,12); //input the file say - file.txt
    
    score=100;
    fout.open(fname); //opens file for writing
    fout<<score; //writes the value of score 
    fout.close(); //closes the file
    
    cout<<"DATA STORED."
    getch();
    
    clrscr();
    cout<<"THE DATA STORED IN : "<<fname<<" IS :\n";
    fin.open(fname); //opens for reading
    fin>>score; //reads the score
    cout<<score; //displays it
    fin.close(); //closes it
    
    // when you open the file 'file.txt' you'll see 100 in it
    getch();
    return 0;
    }

  5. #5
    His posts are far and few Esparno's Avatar
    Join Date
    Mar 2002
    Posts
    100
    Thanks a bunch
    Signature is optional, I didnt opt for one.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Saving Game data algorithm,perhaps?
    By Sentral in forum Game Programming
    Replies: 2
    Last Post: 08-25-2006, 07:14 PM
  2. Saving .IMA files...
    By Ezzetabi in forum C++ Programming
    Replies: 2
    Last Post: 12-18-2005, 12:09 PM
  3. Game Designer vs Game Programmer
    By the dead tree in forum Game Programming
    Replies: 8
    Last Post: 04-28-2005, 10:17 PM
  4. Saving the game - help plz!
    By Gamemaniac in forum Game Programming
    Replies: 19
    Last Post: 02-20-2005, 12:33 PM
  5. Game Save Files
    By drdroid in forum C++ Programming
    Replies: 1
    Last Post: 06-04-2002, 04:02 PM

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