Jump to content
  • Advertisement
Sign in to follow this  
Juliean

fstream relative dir?

This topic is 1953 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello,

 

I'm working on a file import routine for my games editor. When for example loading a texture, I want to copy it to the the resource folder. This is my file copying function:

 

        void FileCopy(const std::wstring& stInName, const std::wstring& stOutName)
        {
            std::ifstream ifs(stInName, std::ios::binary);
            std::ofstream ofs(stOutName, std::ios::binary);

            // todo: add description to the expcetions
            if(!ifs.is_open())
            {
                throw fileException();
            }
            else if(!ofs.is_open())
            {
                throw fileException();
            }

            ofs << ifs.rdbuf();
        }

 

Works fine as long as I specify input and output file as absolut paths, like "C:/Editor/Textures/Out.png", but if I want to use the relative path, like "../Textures/Out.png", it fails to open the output file. Is there any trick or something I left out, or do I really have to retrieve the applications absolute dir and manually convert the relative path? If so, is there any easy, existing solution?

Share this post


Link to post
Share on other sites
Advertisement

Relative paths work for me with G++. If it makes a difference, try using proper backslash separators, and make sure that your current working directory is in C:\Editor\SomeDirectory.

Share this post


Link to post
Share on other sites

Are you using "open file dialog" thing? It probably changes current directory, so your relative paths become relative to the file you just opened, not your .exe.

Share this post


Link to post
Share on other sites

Are you using "open file dialog" thing? It probably changes current directory, so your relative paths become relative to the file you just opened, not your .exe.

 

Ah yes, I was using this, and this obviously caused the issue, thanks. Since I'm using relative directories everywhere, it was very unlikely that it was set wrong from the start on - but the file dialog got me. So I have to store the current path using "GetCurrentDirectory" before using the file dialog and restore it after I'm done using "SetCurrentDirectory", or is there some kind of flag that "fixes" this behaviour?

Share this post


Link to post
Share on other sites

OFN_NOCHANGEDIR is there, but not very useful. You're better off doing it manually.


 

Restores the current directory to its original value if the user changed the directory while searching for files.

This flag is ineffective for GetOpenFileName.

 

I mean, I'll definately try it out once I'm done encapsulating the file dialog into a class for my own gui, but this doesn't sound promising, does it? Or did you try it out yourself, and it worked nevertheless?

 

Edit: Ok, isn't so hard doing it myself given that I encapsulate the file dialoge anyway, but still, a bit odd, I wonder if more people fall for this when going their first steps with the windows file dialog :/

Edited by Juliean

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!