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

Started by
8 comments, last by Zero Point One 22 years, 4 months ago
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
Advertisement
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.
--------------------------I present for tribute this haiku:Inane Ravings OfThe Haunting JubilationA Mad Engineer©Copyright 2005 ExtrariusAll Rights Reserved
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
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)
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?
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
.......-.-'

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
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!
I don''t know C++, but in Delphi you would use TFileStream
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

This topic is closed to new replies.

Advertisement