Thread: Save system...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    673

    Question Save system...

    Ok, This is driving me nuts, and searching google just leads me to dead ends so last resort to ask. I am try to create a file save system, but where the user can specify the name of the file. is this possible using ofstream? this wont work but an example of what I am trying to do
    Code:
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    int main() {
    	 string save = "data";
    	 ofstream out ( + data +".txt" );
    	 cin.get();
    }
    So is there a way to do something like that?
    Thank you SO much

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Correct.

    std::getline()

    Kuphryn

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    umm how would getline() do anything?
    I am trying to make file names editable

    EDIT:
    if getline(0 would work can you show me how please?
    Last edited by Raigne; 07-11-2006 at 05:32 PM.

  4. #4
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    MFC:

    Code:
    ...
    CFileDialog *dlg=new CFileDialog(true,
                                       L"*.dat",
                                       NULL,
                                       OFN_FILEMUSTEXIST,
                                       L"Data files (*.dat)|*.dat|"
                                       L"All files (*.*)|*.*|");
    
      INT_PTR result=dlg->DoModal();
      if (result==IDOK)
      {
         CFile file(dlg->GetPathName(),CFile::modeRead);
         CArchive Ar(&file,CArchive::load);
    
         Serialize(Ar);
      } 
    
    delete dlg;
    ...

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    Doesnt MFC require Microsoft Visual Studio? and will I not have to write my entire code in either MFC or win32 for that to work? Right now I am using a console, just because I cant use string in win32 with making the stupid char arrays, and atoi on all my integers to get them to go on screen. Console is easier because of support for std::string, but if I must go to win32 or MFC I can.. but if MFC requires MSVS then I cant I dont have a supported operating system

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    The question really belongs to c++ programming then.
    Use c_str() in the ofstream declaration

    Code:
    #include <string>
    #include <fstream>
    #include <iostream>
    
    int main()
    {
        std::string fileName;
        std::cout << "Enter file name: ";
        std::getline(std::cin, fileName);
         
    //add extension to fileName
    //however, should test first if the user already typed one
    
        std::ofstream FileOut(fileName.c_str());
        if (FileOut) {....

  7. #7
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    that is what kuphryn was suggesting, but I had no Idea how it worked. Thanks a million, and bubba you answer would be great if I was using MFC

    EDIT: I post in windows, because I use alot of non-standard code, so by posting here non-standard doesnt matter.

  8. #8
    Banal internet user
    Join Date
    Aug 2002
    Posts
    1,380
    Code:
    #include <iostream>
    #include <fstream>
    
    int main()
    {
        using namespace std;
    
        char ToSave[] = "Hello world!";
        char Filename[32];
    
        cout << "Specify filename: ";
        cin >> Filename;
        
        ofstream FileStream(strcat(Filename, ".txt"), ios::out | ios::trunc);
    
        FileStream << ToSave;
    
        cout << "Done.\n";
    
        return 0;
    }

  9. #9
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    Sorry if this is bumping the thread, but I figured this is better than a new thread.
    I was wandering what the best way to create a navigatable enemy system?
    so far I have just been using something like
    Code:
    struct enemy {
       int hp[100];
       int mp[100];
       // and so on for the the stats.
    };
    enemy enemy1;
    enemy enemy2;
    enemy enemy3;
    //one instance for each enemy in battle
    of course this works fine until I try to have the weaker enemies not show up after the character reaches a certain level, because I was just using int x = rand();

    I can tell it how far not to go, but I cant seem to tell it to skip a certain amount, and was wondering if there is a better approach to this? ( and I am also having trouble saving these values to a file, and then loading them ) Thank you

  10. #10
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    Code:
    int hp[100]
    Last time I played games, we didn't need a 100 int array to store their HP... the times have changed I see.

    You will probaly want to start looking into binary saving/reading, it isn't as hard as it sounds.

  11. #11
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    lol, well I have a feeling I am really doing it the hard way huh? I guess Ill look into binary stuff, but I have never been very good at binary

  12. #12
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    Once you get used to it, it is will seem like the logical choice for stuff like this. What you may want to do is make a reader into your game... then a character/enemy editor that is external. Same goes with maps.

  13. #13
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    hmm can you link me to some good references or tutorials? ( I am searching google right now ) but would like some help searching if you dont mind thanks alot

  14. #14
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    I learned what I know of binary saving/reading from decoding the diablo 2 save files and playing around with it, so I don't know any guides, sorry.

  15. #15

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Save vs Save As.... OPENFILENAME flags
    By csonx_p in forum Windows Programming
    Replies: 16
    Last Post: 06-01-2008, 02:42 PM
  2. save and save as functions
    By willc0de4food in forum Windows Programming
    Replies: 2
    Last Post: 06-29-2005, 02:49 AM
  3. save webpage? some save some not
    By kryptkat in forum Tech Board
    Replies: 3
    Last Post: 06-07-2005, 09:21 AM
  4. Ask user to save file before exiting.
    By Bajanine in forum Windows Programming
    Replies: 3
    Last Post: 11-15-2004, 06:34 PM
  5. System Commands Output
    By nasir_qau in forum Linux Programming
    Replies: 2
    Last Post: 03-21-2002, 03:14 PM