Jump to content

  • Log In with Google      Sign In   
  • Create Account

fstream relative dir?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 Juliean   GDNet+   -  Reputation: 2618

Like
0Likes
Like

Posted 19 May 2013 - 12:26 PM

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?



Sponsor:

#2 Ectara   Crossbones+   -  Reputation: 2970

Like
1Likes
Like

Posted 19 May 2013 - 12:42 PM

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.



#3 Zaoshi Kaba   Crossbones+   -  Reputation: 4353

Like
1Likes
Like

Posted 19 May 2013 - 12:43 PM

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.



#4 Juliean   GDNet+   -  Reputation: 2618

Like
0Likes
Like

Posted 19 May 2013 - 12:54 PM

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?



#5 Adam_42   Crossbones+   -  Reputation: 2507

Like
3Likes
Like

Posted 19 May 2013 - 01:03 PM

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


Edited by Adam_42, 19 May 2013 - 01:06 PM.


#6 Juliean   GDNet+   -  Reputation: 2618

Like
0Likes
Like

Posted 19 May 2013 - 01:08 PM

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, 19 May 2013 - 01:12 PM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS