Saving a game to a file...help...please...

This is a discussion on Saving a game to a file...help...please... within the C++ Programming forums, part of the General Programming Boards category; I would like to know how to save certain variables to a file. Well if not certain all variables. Please ...

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    731

    Saving a game to a file...help...please...

    I would like to know how to save certain variables to a file. Well if not certain all variables. Please just tell me were a tut is on how to do this. Thanks!

  2. #2
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    Well really, you would just use the c++ standad i/o stream

    Code:
    #include <fstream>
    
    int var1 = 20;
    int var2 = 30;
    
    int main()
    {
    
        std::ofstream pFile("filename.txt");
    
        pFile << var1;
        pFile << std::endl;
        pFile << var2;
    
        pFile.close();
    
        return 0;
    
    }

    I believe thats correct, I have the worst memory in the world and I usually forget simple things here and there.
    Last edited by Vicious; 08-28-2004 at 10:57 PM.

  3. #3
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    Alright it looks good. But what is the pFile << std::endl; for?

    And do I have to save it as a .txt format?

  4. #4
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    If im not mistaken I THINK you have to do that to insert a new line into the file

    so it will be

    20
    30

    and not

    2030

    Im not sure, i'll look into it.
    I havent done that kind of file i/o in a long while


    And as far as filetype, you can change that sucker to anything you want

  5. #5
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    You can use whatever format you want, but using this method it just has to be an ASCII file, not binary. It's just standard file i/o. pFile << std:end; sends the endl character to the stream represented by pFile.

  6. #6
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    Alright everyone here is so helpful!

    And I get answers just minuts after I post! WOW!

  7. #7
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    That would be space inefficient, and you could (and probably would) lose information that way if you're using doubles. Also, you wouldn't be able to write the contents of an object of a class. Open it in binary mode, and use write() and read().

    Code:
    #include <fstream>
    
    int var1 = 20;
    int var2 = 30;
    
    struct TestClass
    {
       int objVar1;
       double objVar2;
    };
    
    TestClass test;
    
    int main()
    {
        test.objVar1 = 5;
        test.objVar2 = 4.5283928334328;
    
        std::ofstream file("filename.dat", std::ios::binary);
    
        file.write((char*)&var1, sizeof(var1));
        file.write((char*)&var2, sizeof(var2));
        file.write((char*)&test, sizeof(test));
        file.close();
    
        return 0;
    }
    This way, the file's size is 20 bytes (3 ints, 1 double). When you want to load the data then, declare 2 ints and a TestClass, and use file.read() in the same order as you write()'ed the variables. Notice the bolded items: it's ofstream, for "output file stream", just 'file' because p stands for pointer, and I changed it to .dat because you're writing binary data not text, and I added the flag std::ios::binary for writing binary data (that's what the write() does, just outputs the binary data stored in the variable instead of converting the data into a string and writing that).

    Hope this helps!
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  8. #8
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    Alright I think I get yours a little less but it makes more sence. And you told me how to read the file so, lol, I'll use that way.

    But thanks everyone else.

  9. #9
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Oh boy, beaten by like 4 people lol... Anyhoo, here goes:

    You can use whatever format you want, but using this method it just has to be an ASCII file, not binary.
    'format' is the wrong word. Format refers to how you lay out the data within the file, and what goes where. I believe what you mean is 'extension', and that absolutely does not matter (even my change to .dat isn't technically necessary) as far as file i/o goes. It only matters when somebody looks at the file, and they're wondering what it's for... if they see .dat, they know they probably shouldn't mess with it; if they see .dll, they know it holds functions for some program (or several programs); if they see .doc, they know Microsoft Word or WordPad created it.

    it just has to be an ASCII file, not binary
    No it doesn't, you can have a binary file with .txt extension (as I explained above). If you want proof, go to my website (link in the signature), download asv.zip from the DigiBible page, unzip it and open it in notepad or a hex editor... it is most assuredly binary.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  10. #10
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    I figured I would let Rune absorb the basic idea of writing a few variables to a file before I went into this... jeez hunter... ruin it for me will ya... and you code isnt even color coded.. jeez... punk



    lol, j/k, ofcourse.

    Yes hunter's method is a MUCH better way of writing to a file.
    Stuff like File I/O is when you really need a good book.

    I personally like the explanations in C++ Black Book

  11. #11
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Sorry, didn't mean to steal your fire

    But anyhow, yes if you want to just write a few variables out in some way that will let any hojo jojo mofo look at the file in notepad and understand what you wrote, use Vicious' way... you can use ofstreams pretty much in the same way as you would use cout, it's just that when you're saving a game it's not always the best way of doing it unless you want the user to be able to modify the file in notepad. If you want to write pure data though (the actual variables, without converting them to strings), use my method.

    >>I get yours a little less
    Yes, you'll need to look at the actual function's parameters. I think write() and read() both take (char*, int); the char* is just a pointer to any location in memory - whether it's a structure, an array, a string, or just an int. The int parameter is the number of bytes to read/write, starting from the location in memory pointed to by the char*. So if you're writing a variable to the file, the number of bytes you're writing is the size of the variable, starting from the address pointed to by the char*. Thus,

    file.write(&theTestClassObject, sizeof(theTestClassObject));

    But the function takes a char*, so you need to convert the theTestClassObject* to char*:

    file.write((char*)&theTestClassObject, sizeof(theTestClassObject));

    I hope this clears it up a little bit, although I did use some big words... if you don't understand anything, just say so and I'll try to explain better.

    **EDIT**
    Err, forgot to mention... when you're reading from the file again, you need to use an ifstream, for input file stream - not ofstream.
    Last edited by Hunter2; 08-28-2004 at 11:24 PM.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  12. #12
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    file.write(&theTestClassObject, sizeof(theTestClassObject));

    But the function takes a char*, so you need to convert the theTestClassObject* to char*:

    file.write((char*)&theTestClassObject, sizeof(theTestClassObject));
    That accauly made it more confusing but but after looking at it for a couple of mins I get it all now.

    Thanks everyone, again!

  13. #13
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    And wow I opened the .dat file with a hex editor and you can edit pretty essily. But I don't care that much.

  14. #14
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>And wow I opened the .dat file with a hex editor and you can edit pretty essily.

    Yup, that's how it is with files, they're really easy to modify. That's why you see so many "trainers" or "hacked saves" for games out, although for most commercial games they try to make it a little harder by encoding things

    I'm glad you got it figured out
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  15. #15
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    Is anyone else here freakishly impressed with Rune's amazing ability to learn this crap?

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 05:27 PM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  3. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 04:44 PM
  4. Game Engine Link Prob
    By swgh in forum Game Programming
    Replies: 2
    Last Post: 01-26-2006, 12:14 AM
  5. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 11:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21