Archived

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

Newbie Question.........

This topic is 5885 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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!!

Share this post


Link to post
Share on other sites
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.


Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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!!

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
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!!

Share this post


Link to post
Share on other sites
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 <fstream>
#include <iostream>
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;
}

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
savagerx: why are you get()''ing from an output stream? (I''ll assume that was a typo). Also, you only get up to the length of your str (buffer) characters of data

To read in a "page" of data (what is a page? Let''s assume a page is 25 lines for this discussion - or 1024k in 80x25?)

ifstream in;
in.open("filename.ext");
if(in.fail())
{
cout << "failed to open filename.ext" << endl;
return -1;
}
char str[255]; // be excessive
while(!in.eof())
{
in.getline(str, 255, ''\n''); // break on newline
if(in.fail())
break;
// save data in str somewhere; my favorite place is in a vector
// this time though, we''ll just print it to the screen:
cout << str << endl;
}

Share this post


Link to post
Share on other sites