Code::Blocks

User forums => Using Code::Blocks => Topic started by: Crumbler on September 16, 2020, 08:49:50 pm

Title: Problem with relative file path
Post by: Crumbler on September 16, 2020, 08:49:50 pm
I'm writing a program using C++.
In one of my functions I need to open a .txt file using an std::ifstream.
This file is supposed to be located in the same folder as the executable.
I'm using a relative path.
However when launching my program directly from CodeBlocks, it can't find the file.
If I copy the .txt file into ...\bin\Debug and launch the executable manually, it works just fine.
Absolute paths work fine from CodeBlocks.
How can I make the relative path work when launching my program directly from CodeBlocks?
Title: Re: Problem with relative file path
Post by: Miguel Gimenez on September 16, 2020, 09:11:55 pm
You must go to Project -> Properties -> Buil targets and set "Execution working dir" to bin\Debug
Title: Re: Problem with relative file path
Post by: Crumbler on September 16, 2020, 09:58:39 pm
Thanks, this works for me.
Though I still have to get the text file copied to the folder when I build the project.
I used a post-build step with a cmd command:

cmd /c copy $(PROJECTDIR)\fShader.txt $(TARGET_OUTPUT_DIR)
cmd /c copy $(PROJECTDIR)\vShader.txt $(TARGET_OUTPUT_DIR)

Is there any way to do that more elegantly?
Title: Re: Problem with relative file path
Post by: Miguel Gimenez on September 16, 2020, 10:33:08 pm
If your program does not modify the files you need to copy them only once.
Title: Re: Problem with relative file path
Post by: sodev on September 16, 2020, 11:38:51 pm
Set the Execution working dir to the directory where the files reside, this way you dont need to copy them around.