Skip to main content
GameDev.net gamedev.net
🔒 Locked

Newbie Question.........

Started by Android 13 Nov 1, 2001 at 6:19 PM 13 replies 1.7k views
Original Post
Android 13
Android 13
I know this may sound dumb, but how do you save in C++. I looked in my books, but I cant find out how to save in it. Im relativley new to it, so i was hopeing you could help me. Thank you. Mess with the best, Die like the rest!!
Mess with the best,Die like the rest!!
buzzy_b
buzzy_b
C++ source files are just normal text files. You can make and edit them in any ANSI text editor.

--Buzzy
Android 13
Android 13
No, I mean thank you for answering, but what I ment was how do you code C++ to save. Like what like of code would I use.

Mess with the best,
Die like the rest!!
Mess with the best,Die like the rest!!
kreyg
kreyg
Look for open/read/write/close, or fopen/fread/fwrite/fprintf/fclose.

Some of the parameters are tricky (ensuring you set the "create" bit for a new file in the open command, for instance) so read the docs carefully.


shizik
shizik
if you mean,
"how do i save a bunch of data so that the program can get the data the next time it runs?"

then, what you have to do is save your data to a textfile or to a data(binary) file.

check out "fstream"
or "file i/o" in any c++ book
or search for tutorials on these online
Android 13
Android 13
What im wondering is like if you were in a program, and you wanted to save your progress. What line/lines of code would you use to let the user save thier progress. I dont know if it helps but im compialing in Dev C++.

Mess with the best,
Die like the rest!!
Mess with the best,Die like the rest!!
deadlinegrunt
deadlinegrunt
What you looking for has the potential of being extremely complex or very simple. It all depends on your needs. What you are asking is "How do I save a game state?" This depends on a lot of different factors. The best advice I could give you is to first learn fundamentals of file i/o with simple Hello World type programs that save to disk in various formats. Once you see the many different ways you can save actual data itself coupled with object data structure persistance you''ll understand why this question is a can of worms best left un-opened for now till you''re absolutely ready for those concepts.

YAP-YFIO

-deadlinegrunt
~deadlinegrunt
Necrosis
Necrosis
It''s not compiler dependant. The concept of saving is storing your data into another file (such as player''s position, player''s score, etc) and then "loading" a saved game involves reconstructing the game with the data in the file that was saved to.

Hope that helps.

If you can read this, All your base are belong to us!
Android 13
Android 13
I know some C++, Ill give you an ecample of the things i know, here is my first game. Warning: Very Basic.


//Monster Fighter is a basic battle program to
//Practice C++

#include

int userLvl;
int monsterLvl;
int f = 0;
int main()
{
//white space, specification term to center text here

cout << "Welcome to Monster Fighter V 1.0 created by: Adam Osterkamp.\n";


cout << "Please enter your Power Level 1,000 - 10,000: ";
cin >> userLvl;

begin:
cout << "Please choose a Monster Level between 1-5: ";
cin >> monsterLvl;

if (monsterLvl < 1 || monsterLvl > 5)
{
cout << "You cant do that! Restart the Program Fool!";
cin.ignore(256,''\n'');
cout<<"press enter to continue..."< cin.get();
goto begin;
}

if (monsterLvl == 1)
{
monsterLvl *= 1000;
}

if (monsterLvl == 2)
{
monsterLvl *= 1000;
}

if (monsterLvl == 3)
{
monsterLvl *= 2000;
}

if (monsterLvl == 4)
{
monsterLvl *= 2500;
}

if (monsterLvl == 5)
{
monsterLvl *= 3000;
}

while (f == 0)
{
if (monsterLvl < userLvl)
{
cout << "WOW! Your powerfull! You beat the Monster, Good Job!!";
cin.ignore(256,''\n'');
cout<<"press enter to continue..."< cin.get();
}

if (monsterLvl > userLvl)
{
cout << "HA! Your weak, get stronger and try again!";
cin.ignore(256,''\n'');
cout<<"press enter to continue..."< cin.get();
}
f++;
}

return 0;
}

There is a small bug, but it runs the way it is supposed to, unless user error occers, which im fixing. But lets say, if you lost this battle you get 30 on your PL and if you win you get 60. Now i know i would have to add some code in there to do this but then if i wanted to save that info, so later i could ask for your character, how would i do that?

Mess with the best,
Die like the rest!!
Mess with the best,Die like the rest!!
Clovis2000
Clovis2000
Hey, Android, I think I saw you at MGS2 board and www.gamefaqs.com???
Say, you have any good link for MGS2 screenshot?
Nothing is impossible if you don''t take it seriously...And nothing is possible if you take it seriously.
Android 13
Android 13
Sorry man wasnt me. I assume your talking about Metal Gear Solid 2? But it wasnt me. Sorry.

Mess with the best,
Die like the rest!!
Mess with the best,Die like the rest!!
Oluseyi
Oluseyi
quote:
Original post by Android 13
What line/lines of code would you use to let the user save thier progress.

That''s a complex topic (not the saving - the progress bit). Essentially you need to save all important variables and statistics so that when the user loads it back, everything is exactly as they left it.

Anyway, saving a file:

  1. Create a file stream object. Output file streams are good enough.

  2. Open the file to save to; if you want to create the file if it doesn''t already exist, be sure not to set the ios::nocreate bit.

  3. Write the data out using the standard insertion operator (<<), just as you would with cout.

  4. Close the file.



Code to do it? Man, that''s half the fun (figuring stuff out)!

Okay, I relented:

#include &ltfstream>
#include &ltiostream>
using namespace std;
//
int main(int argc, char *argv[])
{
ofstream ofile;
// this will open the file if it exists and overwrite its contents:
ofile.open("filename.ext");
// to append (add to the end) data to the file, use this:
// ofile.open("filename.ext", ios::app);
//
// make sure the file opened correctly:
if(ofile.fail())
{
cout << "Could not open filename.ext!" << endl;
return -1;
}
// you can now write data out just like cout (the beauty of stream objects)
int i = 42;
float f = 42.00f;
char c = 42;
ofile << i << endl // save the integer to the file; newline
<< f << endl // save the float to the file; newline
<< c << endl; // save the char to the file; newline
// close the file:
ofile.close();
return 0;
}
Android 13
Android 13
Thanks alot man!!

Mess with the best,
Die like the rest!!
Mess with the best,Die like the rest!!
savagerx
savagerx
But, how do we write a page of data? Is it very advanced?

What I''ve used and failed:
  
ofstream out;
for(int x=0;x<strlen(str);x++){
out.get(*str[x]);
}
out.close();


It seems that with these, I can only get a few lines of input.
I welcome any comments and advice.
The road may be long, wind may be rough. But with a will at heart, all shall begone. ~savage chant

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.