Jump to content
  • Advertisement

Archived

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

Android 13

Newbie Question.........

This topic is 6195 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

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

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!