Newbie Question.........

Started by
13 comments, last by Android 13 22 years, 5 months ago
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!!
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 &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;} 
Thanks alot man!!

Mess with the best,
Die like the rest!!
Mess with the best,Die like the rest!!
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
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 excessivewhile(!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;} 

This topic is closed to new replies.

Advertisement