Archived

This topic is now archived and is closed to further replies.

Zero Point One

How do you implement "Save" and "Load" options?!

Recommended Posts

How do you implement "Save" and "Load" options in a msdos based C++ program?! How do I do this?!? Could you write out the code for me leaving "" around the areas that I know need to be replaced by variables, file directories, etc. Is it a long code? Or maybe you could just send me a link to a site that explains it. I need to use these for an RPG game I'm creating for my Computer Science project over Christmas break! Edited by - Zero Point One on December 9, 2001 10:15:40 PM

Share this post


Link to post
Share on other sites
Just find the variable that define game state and write them to a file. Just read them in to load a game. For exapmle:
write the characters position to a file, what puzzles have been solved, levels,score,etc..Thats probably the easiest way.

Share this post


Link to post
Share on other sites
Just use <file.h>. Using fopen(), fread(), fwrite(), and fclose().

It's pretty simple and planet-source-code.com probably has tons of examples.

[Edit: My <file.h> didnt show because of HTML]

Edited by - Mark_C on December 9, 2001 10:30:03 PM

Share this post


Link to post
Share on other sites
if you dont know how to write/read to/from a file then you hsould not be coding an rpg game. (ie just write out what you need to save in order, and read form the file in the SAME order)

Share this post


Link to post
Share on other sites
Could you explain how to use the fopen(), fread(), fwrite(), and fclose() a little more clearly? Do you have any examples yourself? I looked on that site you had posted and I couldn''t find much, either that or I just got lost in finding anything.

How would I use fopen? fopen(directory/filename.something)?
I''m sure your probably laughing but I''m only a beginner so cut me some slack. Also when I use fwrite() can that write a file that is of my own custom format/extension. And one last thing, wouldn''t I use ofstream, ifstream, and fstream for inputing and outputing files, or is your way much easier to follow?

Share this post


Link to post
Share on other sites
fwrite and fread are better with binary data but ifstream and ofstream are a little easier to use.

this is off the top of my head and untested but...

//////////////
#include
#include
//////////////

bool loadPlayer(char *playerFile, player *characterData)
{

FILE *f = fopen(playerFile,"r"); // second arg is access mode "r" = read
if (!f)
return false;


memset(characterData,0,sizeof(struct player));
// fread(&to,sizeeach,amount,file)
fread(characterData,sizeof(struct player),1,f);

fclose(f);
return true;
}

[Edit: HTML is annoying ]

Edited by - Mark_C on December 11, 2001 7:10:49 PM

Share this post


Link to post
Share on other sites
.......-.-'

Maybe I shouldn't have asked. I can kinda follow what your writing there, but I'm too much of a beginner to understand all that. Doesn't mean I can't, I'm sure if it were my teacher or someone going over it I would, but its alot of commands I have no idea what they do. Anyway to make that more clearer? Here's really an example of what I'd like to do:

--------------------------------------------------------
You have reached a saved point, would you like to save?
Enter your choice (Y or N): y

Your game has been saved!

--------------------------------------------------------

I noticed in your function was called loadplayer, though all I really want to do is load a file that contains all the information up to the save point, or maybe that is what you meant by loadplayer. HelP?

Edited by - Zero Point One on December 9, 2001 11:15:12 PM

Share this post


Link to post
Share on other sites
quote:
Original post by Zero Point One
...I''m too much of a beginner to understand all that.

This statement is key. You''ll need to learn quite a bit to write any type of game more complex than "guess the numbers" or hangman. I suggest you pick up a good C/C++ book or find one of the many tutorials floating around the net (Google is your friend), take some time off trying to make a game, and really learn essential basics of programming. Then go back to your game and realize how much easier it will be to do a number of things.

Don''t worry about time; GameDev will still be here.

[ GDNet Start Here | GDNet FAQ | MS RTFM | STL | Google ]
Thanks to Kylotan for the idea!

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I don''t know C++, but in Delphi you would use TFileStream

Share this post


Link to post
Share on other sites
Also, when trying to learn something new, don't read 30 lines of source code and expect to be have the ability to implement it without any problems (at least, not always). Try breaking down what the code is doing and experiment with each thing.

You could start by making a program that outputs "Hello World" to a text file. When that is working, read it from the file and display it on the screen. When that is working, try saving a struct or class in binary mode. Then load that back in from the file and display it's contents. Baby steps my friend. Baby steps is the key.

PS - By the way, This Site is a great reference for C/C++. Just look under "stdio". Good Luck


EDIT - Added PS and messed up a bunch of times. Damn messageboard...

Edited by - Big Sassy on December 11, 2001 7:01:23 PM

Edited by - Big Sassy on December 11, 2001 7:07:28 PM

Share this post


Link to post
Share on other sites