Author Topic: Problem with relative file path  (Read 440 times)

Offline Crumbler

  • Single posting newcomer
  • *
  • Posts: 2
Problem with relative file path
« 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?

Offline Miguel Gimenez

  • Lives here!
  • ****
  • Posts: 569
Re: Problem with relative file path
« Reply #1 on: September 16, 2020, 09:11:55 pm »
You must go to Project -> Properties -> Buil targets and set "Execution working dir" to bin\Debug

Offline Crumbler

  • Single posting newcomer
  • *
  • Posts: 2
Re: Problem with relative file path
« Reply #2 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?
« Last Edit: September 16, 2020, 10:02:25 pm by Crumbler »

Offline Miguel Gimenez

  • Lives here!
  • ****
  • Posts: 569
Re: Problem with relative file path
« Reply #3 on: September 16, 2020, 10:33:08 pm »
If your program does not modify the files you need to copy them only once.

Offline sodev

  • Regular
  • ***
  • Posts: 405
Re: Problem with relative file path
« Reply #4 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.