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

Author Topic: need help with a weird problem (working with files)  (Read 6662 times)

0 Members and 1 Guest are viewing this topic.

Assassinbeast

  • Jr. Member
  • **
  • Posts: 62
    • View Profile
    • Email
need help with a weird problem (working with files)
« on: November 06, 2012, 11:18:54 pm »
Hello everybody  :)
I have the weirdest problem when im working with files and saving them as highscore.

It took me about 3 hours to figure out why sometimes it would save the highscore on the harddrive, and why sometimes it woulnd't when i have to release and make an installer for it.

It works perfectly when im debugging it on my own computer, but not when i make an installer for it.

So my question is, is this how to work with files?

#include <SFML\Graphics.hpp>
#include <fstream>
#include <string>
#include <sstream>

sf::RenderWindow mywindow(sf::VideoMode(800,600,32),"ddsada");
sf::Event ev;
sf::Font myfont;

sf::Text t_score;
sf::Text t_highscore;
int score = 0;
int highscore = 5;


template <typename T>
std::string toString(T arg)
{
        std::stringstream ss;
        ss << arg;
        return ss.str();
}

int main()
{
        mywindow.setVerticalSyncEnabled(true); //60 fps
        myfont.loadFromFile("thefont.ttf");
        t_score.setFont(myfont);
        t_highscore.setFont(myfont);
        t_highscore.setPosition(0,50);

        std::ifstream inputfile("highscore.txt");
        inputfile >> highscore;
        inputfile.close();

        while(mywindow.isOpen())
        {
                t_score.setString(toString("Score= ") + toString(score));
                t_highscore.setString(toString("Highscore= ") + toString(highscore));

                while(mywindow.pollEvent(ev))
                {
                        if(ev.type == sf::Event::Closed || ev.key.code == sf::Keyboard::Escape)
                                mywindow.close();
                }
                mywindow.clear(sf::Color(0,200,0));

                score++;
                mywindow.draw(t_score);
                mywindow.draw(t_highscore);

                mywindow.display();
        }

        if(score > highscore)
        {
                std::ofstream outputfile("highscore.txt");
                outputfile << score;
                outputfile.close();
        }

}
 

So when i make an installer for it... it actually doesn't work when i include the highscore.txt (and the dll files) and make the installer for it.

It only works when i include the files i need (the dll files) and after i install it, then i have to make a new highscore.txt file (eg. in my deskop) and then put it in my c drive where the program actually is (where the font and dll files are).


Sorry, theres alot of reading and text. But im very very close to make my game fully functional and i want to show it to you all  ;D.

I just cant make this file thing to work  >:(
« Last Edit: November 06, 2012, 11:27:38 pm by Assassinbeast »

mateandmetal

  • Full Member
  • ***
  • Posts: 171
  • The bird is the word
    • View Profile
    • my blog
Re: need help with a weird problem (working with files)
« Reply #1 on: November 07, 2012, 01:00:37 am »
1) What installer are you using?
2) Is your installer really copying all the files needed?
3) Do you need administrator rights on the destination folder?
4) You should check if your file can be opened

  std::ifstream inputfile("highscore.txt");
  if (!inputfile.is_open()) { // Error }  
 

5) Why you convert to string something that already is a string?

  t_score.setString(toString("Score= ") + toString(score)); // Non sense
  t_score.setString("Score= " + toString(score));
 

6) "then i have to make a new highscore.txt file "
Check if the file can be open. If not, create it. If it cannot be created, maybe the user needs admin rights, or maybe the disk is full?  ???  :)
- Mate (beverage) addict
- Heavy metal addict _lml
- SFML 2 addict
- My first (and free) game: BichingISH!

Sui

  • Jr. Member
  • **
  • Posts: 67
    • View Profile
    • http://www.suisoft.co.uk/
Re: need help with a weird problem (working with files)
« Reply #2 on: November 07, 2012, 09:05:22 am »
Which Operating System are you installing on? Is it Windows?
Gary Marples, Developer of games and other stuff.
www.suisoft.co.uk/games/BlogTwitter

Assassinbeast

  • Jr. Member
  • **
  • Posts: 62
    • View Profile
    • Email
Re: need help with a weird problem (working with files)
« Reply #3 on: November 07, 2012, 04:49:50 pm »
Im using windows and vc 2010.

1) What installer are you using?

Advanced Installer

2) Is your installer really copying all the files needed?

I included all the dll files e.g sfml-graphics-2.dll , and all the 6 other dlls (so 7 dll.s)
And then i include the my font and highscore.txt. But if i include the highscore.txt, and make the installer, then i can only open it, but not save it. I know i can open highscore.txt because i put "1" in highscore.txt and it shows highscore: 1 (and not 5 because thats what i made it default).

And if i dont include the highscore.txt, then i make the installer, install on my computer and then i have to make a new highscore.txt (e.g on my desktop) and put it in my c drive where i installed the program (where the font and cpp is).

Then it can save and work perfectly
Sooooooo strange  :-\

3) Do you need administrator rights on the destination folder?

I dont really know what u mean by that. But i couldn't change the highscore.txt directly inside the program(where the fonts and dlls are), but i could make a highscore.txt(on the desktop) and then put it in the folder where the current highscore.txt is and replace it with the new one( which works and its like cheat if u want to change the highscore).
But i think it was the administrator rights when i had to do that, because i get a message and it said something like that.

4) You should check if your file can be opened

Yes, it opens, but its not saving. (only if i make the installer and dont include the highscore.txt, and after i install it, i have to make a highscore.txt in my desktop and put it in the program(where the font and dlls are) THEN IT WORKS.... SOOOOOO STRANGE


im so confused right now :/
Also... can u recommend me some other installers that i should try out, maybe that would solve the problem  :-\ Because it works when im debugging, but not when i have installed it and run it.

But thanks alot, i appreciate your help so so much  :)
« Last Edit: November 07, 2012, 04:55:07 pm by Assassinbeast »

Sui

  • Jr. Member
  • **
  • Posts: 67
    • View Profile
    • http://www.suisoft.co.uk/
Re: need help with a weird problem (working with files)
« Reply #4 on: November 07, 2012, 06:18:11 pm »
Where does your application get installed to?

If it's C:\Program Files that is a protected folder and you won't be able to write to it. On my old game I had INNO Setup make the folder read/write but this really isn't the right way.

Application data files should be written to the 'Application Data' area or perhaps 'My Documents' to comply with Windows standards.

I created a utility class to do this, as I don't think SFML (at least 1.6) has built in support for file IO to the correct areas.

Let me know and I can post some Mac & Windows compatible code. I haven't made a Linux build of my SFML game as yet.

Perhaps standard functions to get user writable folder paths might be a good feature in SFML, unless I've missed them and they are already there...
Gary Marples, Developer of games and other stuff.
www.suisoft.co.uk/games/BlogTwitter

Assassinbeast

  • Jr. Member
  • **
  • Posts: 62
    • View Profile
    • Email
Re: need help with a weird problem (working with files)
« Reply #5 on: November 07, 2012, 10:56:54 pm »
I just tried to use Inno Setup, and the same thing happends
I think the main problem is that the highscore.txt cant be changed.

So i have to make a new highscore.txt on the desktop and put it in the program and replace it with a normal txt file that can be changed

i can try to make a vid and show you exactly how strange this problem is :S

How can i upload files on the forum? because i really wanna show it  :P

big_totoro

  • Guest
Re: need help with a weird problem (working with files)
« Reply #6 on: November 07, 2012, 11:11:42 pm »
You can attach files by going to 'attachments and other options' underneath the text box when you are making a post. There is a 192kb limit though so I don't think you could really post a video.
I would just upload to youtube or something and link to it.  :)

Assassinbeast

  • Jr. Member
  • **
  • Posts: 62
    • View Profile
    • Email
Re: need help with a weird problem (working with files)
« Reply #7 on: November 08, 2012, 12:08:29 am »
i uploaded it on mediafire   :)

http://www.mediafire.com/?fznd6a4oirocrwp

So at the end, i just showed that it was possible to edit the highscore.txt when i copied it into my programs folder. And also, i couldn't edit the original txt. file. The game could load it, but not save anything in it.

Hope this video will show you exactly what horrible thing ive done wrong  ;D
But also, i want to make it unable to change the txt file or replace it with other txt files. How do i do that? Because my friend is probably gonna do it if he finds out its possible  ::)

Assassinbeast

  • Jr. Member
  • **
  • Posts: 62
    • View Profile
    • Email
Re: need help with a weird problem (working with files)
« Reply #8 on: November 09, 2012, 09:19:36 pm »
Help anyone?  :'(
Its just saving a highscore.... how hard can it be?

http://www.mediafire.com/?fznd6a4oirocrwp

i made this vid to show exactly how strange this is...
In the vid... it looks like the highscore.txt cant be changed by me or by the program... thats what i think it is  :-\

So when i made  a new highscore.txt on the desktop and replaced it with the new one, then the original highscore.txt that was protected and couldn't be changed, got deleted and replaced with a highscore.txt that can be modified by me and the program.

but its just a guess  :(

Aint anybody making games and work with files like me? i mean... come on, help me!!!  :-\

mercurio7891

  • Jr. Member
  • **
  • Posts: 89
    • View Profile
Re: need help with a weird problem (working with files)
« Reply #9 on: November 09, 2012, 09:35:56 pm »
Your video upload is wrong. You uploaded the shortcut instead of the actual video.

Where are you writing the file to? As Sui stated, have you tried writing to My Documents or Application Data instead?

Assassinbeast

  • Jr. Member
  • **
  • Posts: 62
    • View Profile
    • Email
Re: need help with a weird problem (working with files)
« Reply #10 on: November 09, 2012, 10:15:43 pm »
Your video upload is wrong. You uploaded the shortcut instead of the actual video.

Where are you writing the file to? As Sui stated, have you tried writing to My Documents or Application Data instead?

Ahh... sui said if i install it to c/ drive, its wrong. Thats what i did. so i think thats the case why i cant save my new data in the highscore.txt as hes saying that its a protected folder.

So i just tried to install the highscore.txt to my documents. But my program wont load it because the file is in my documents folder.

What code should i write so my program know that it should load the highscore.txt in the documents folder?

mercurio7891

  • Jr. Member
  • **
  • Posts: 89
    • View Profile
Re: need help with a weird problem (working with files)
« Reply #11 on: November 10, 2012, 05:35:18 pm »
It is quite long since i used it. Not sure if it is the same. In windows there is this function call

SHGetKnownFolderPath(...)

You can use that function to get the path to the user documents folder.

Here is a link to msdn. Do note this would need vista and above

http://msdn.microsoft.com/en-us/library/bb762188%28VS.85%29.aspx

regards

Assassinbeast

  • Jr. Member
  • **
  • Posts: 62
    • View Profile
    • Email
Re: need help with a weird problem (working with files)
« Reply #12 on: November 10, 2012, 11:11:57 pm »
It is quite long since i used it. Not sure if it is the same. In windows there is this function call

SHGetKnownFolderPath(...)

You can use that function to get the path to the user documents folder.

Here is a link to msdn. Do note this would need vista and above

http://msdn.microsoft.com/en-us/library/bb762188%28VS.85%29.aspx

regards

wow... i have no idea how to use that  :-\
can you show me how write and use the code plz?  ::)

mercurio7891

  • Jr. Member
  • **
  • Posts: 89
    • View Profile
Re: need help with a weird problem (working with files)
« Reply #13 on: November 11, 2012, 04:42:55 am »
//
// Common values for folder_id
//     FOLDERID_Documents
//     FOLDERID_LocalAppData
//     FOLDERID_RoamingAppData
//     FOLDERID_ProgramData
//
sf::String GetSpecialFolderPath(REFKNOWNFOLDERID folder_id)
{
    LPWSTR wide_string = nullptr;

    if (FAILED(SHGetKnownFolderPath (folder_id, KF_FLAG_CREATE, nullptr, &wide_string)))
    {
         throw std::bad_typeid;
    }

    sf::String path(wide_string);

    // Free the memory that was allocated by the function
    CoTaskMemFree(wide_string);

    return path;
}
 

I think something like that will do. There might be error as I am typing from memory. For the exception, you can throw any exception you like or just abort the function. Depending on your choice of error handling mechanism.

P.S I no longer have a windows machine. So let me know if you are getting any error. I assume sf::String can take wide strings. If it can't then just use std::wstring.
« Last Edit: November 11, 2012, 04:46:16 am by mercurio7891 »

Sui

  • Jr. Member
  • **
  • Posts: 67
    • View Profile
    • http://www.suisoft.co.uk/
Re: need help with a weird problem (working with files)
« Reply #14 on: November 12, 2012, 10:03:23 am »
I tend to use SHGetFolderPathA instead, as this works on Windows 2000 and XP also. There are still a lot of people out there running Windows XP.

The code I use to get the path is:

        const char *PlatformCompatibility::GetUserStorageFolderRoot()
        {
//              return "";              // TEST CODE - simulate obtaining path failed

#ifdef WIN32
                static char appDataPath[MAX_PATH+1];
                memset (appDataPath, 0, MAX_PATH+1);

                // Getting the local appdata path (not the roaming one)
                // Always get the default path - current path should not be relevant for local (non roaming app)
                if (SHGetFolderPathA( NULL, CSIDL_LOCAL_APPDATA, NULL, SHGFP_TYPE_DEFAULT, appDataPath ) == S_OK)
                        return appDataPath;
                else
                        return "";

#else
                // Apple's "Where to Put Application Files" page
                // http://developer.apple.com/library/mac/#documentation/MacOSX/Conceptual/BPFileSystem/Articles/WhereToPutFiles.html

                static string appSupportPath;

                char *tempEnv = getenv("HOME");
                if (tempEnv != 0)
                {
                        appSupportPath.assign(tempEnv);
                        appSupportPath.append("/Library/Application Support");
                        return appSupportPath.c_str();
                }
                else
                        return "";
#endif
        }


You can read more about the function on MSDN:

http://msdn.microsoft.com/query/dev10.query?appId=Dev10IDEF1&l=EN-US&k=k(%22SHLOBJ%2fSHGETFOLDERPATHA%22);k(SHGETFOLDERPATHA);k(DevLang-%22C%2B%2B%22);k(TargetOS-WINDOWS)&rd=true


Note that you will need to create your own folder structure in there before writing to it. It is usual to create a company folder and a unique game subfolder (e.g. Suisoft\Starfire_2012aefr in my case).

On Windows you can use CreateDirectoryA and mkdir is fine for other platforms.

The folder paths will need to be built using string concatenation using the user appdata path as a basis.

Hope this helps.
Gary Marples, Developer of games and other stuff.
www.suisoft.co.uk/games/BlogTwitter