Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: can't save game on install dir  (Read 1735 times)

0 Members and 1 Guest are viewing this topic.

Lkcynric

  • Newbie
  • *
  • Posts: 17
    • View Profile
can't save game on install dir
« on: June 28, 2017, 03:17:42 pm »
I'm not sure if its ok to ask a non SFML question here (the "read before posting" didn't say), if it isn't please tell me (so I learn) and I will delete it myself.

I finished my SFML test game (a snake clone), it was a fun and enjoyable experience. But I have a small problem I would like help with.

After installing the game, I can only save my game score if I run the game as admin mode. It reads it fine, but doesn't write.
- I created the install files using installshield on visual studio 2015.
- I create the file using ofstream
- the file is created in the install folder (install folder: C:\Program Files (x86)\Lkc)
- I'm using windows 10.
- Can't write to file even if the file already exists.
- Its a .dat file.
- it works fine on the visual studio, not sure if because it runs in admin mode, or because its saved at my docs.

Whats the best way to solve this problem?
Should I try grant the file permission to write? (To be honest after googling it for hours, I have no clue how to do that).
Should I save the file in some other place? Where would it be best/usual? (After googling it, no success using "CreateDirectory", can't give it a path and no example online worked for me, they're too old I think)

If anyone cal help I would really appreciate it.

Thank you for the time you took to read this.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: can't save game on install dir
« Reply #1 on: June 28, 2017, 03:39:34 pm »
The "Program Files" directory requires admin privileges.

You have basically two solutions:

1. Install elsewhere if you want to keep it self-contained

2. Write to the standard app data location of your OS; on Windows this is in C:/Users/<USER>/AppData/Roaming/<APPNAME>
Laurent Gomila - SFML developer

Lkcynric

  • Newbie
  • *
  • Posts: 17
    • View Profile
Re: can't save game on install dir
« Reply #2 on: June 28, 2017, 04:47:49 pm »
I see, so thats indeed why.
I will try writing it there, since learning how to do so is useful.

Thank you!

Lkcynric

  • Newbie
  • *
  • Posts: 17
    • View Profile
Re: can't save game on install dir
« Reply #3 on: June 29, 2017, 07:10:50 pm »
Mind taking a look at how I created the folder to see if there's anything really wrong with it?

Its working (at least on my computer, didn't have a chance to test it on another) but its my first time using these and to be honest I don't fully understand it. It was kinda of a nightmare to get it to work, since there is so many variations out there. I had no idea which one to use nor WHY use it.
- getfolderpath | getknownfolderpath | getspecialfolderpath | GetEnvironmentVariable

Anyway as long as its not really wrong, I will leave it as it is and add this to the list of thing I really need to learn about in the future.

        TCHAR szPath[MAX_PATH];
        if (SUCCEEDED(SHGetFolderPath(NULL, CSIDL_APPDATA, NULL, 0, szPath)))
        {
                // Append product-specific path
                PathAppend(szPath, _T("\\Lkc\\"));
                CreateDirectory(szPath, NULL);
        }
        PathAppend(szPath, _T("saveData.dat"));
        std::string hhh;
        hhh.clear();
        for (int i = 0; i < MAX_PATH; i++) {
                hhh += szPath[i];
        }
        vEcras[5]->fichPath = hhh;
        vEcras[3]->fichPath = hhh;
        vEcras[1]->fichPath = hhh;