User forums > Using Code::Blocks
Problem with relative file path
(1/1)
Crumbler:
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?
Miguel Gimenez:
You must go to Project -> Properties -> Buil targets and set "Execution working dir" to bin\Debug
Crumbler:
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?
Miguel Gimenez:
If your program does not modify the files you need to copy them only once.
sodev:
Set the Execution working dir to the directory where the files reside, this way you dont need to copy them around.
Navigation
[0] Message Index
Go to full version