fstream relative dir?

Started by
4 comments, last by Juliean 10 years, 11 months ago

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?

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.

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.

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?

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

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 :/

This topic is closed to new replies.

Advertisement