I've run into a problem on Linux systems that I can't seem to find any concrete solution online for.
I have a C++ application, lets call it Program1
. Program1
opens a text file for reading. The text file is in it's parent directory one level up (../test_file.txt
). So in code, to open the file:
ifile.open("../test_file.txt");
If you open a terminal in the same directory as Program1
and run the executable:
$ ./Program1 &
Everything works fine. test_file.txt
is read without an issue.
Now let's introduce a shell script in a different directory, lets call it ShellScript1
. This script execute Program1
and other programs in different directories. The important line the shell script file looks something like this:
../../test/Release/Program1 &
When this shell script is ran Program1
executes but test_file.txt
cannot be read. Now as far as I can tell this is because the active directory is the directory of the shell script.
I need to be able to run executables with a shell script that doesn't actively change the relative pathing used in the executable such as with Program1
when ran via shell script.
Is there a way around this? Or is my method of reading in the file in my code need improvement?
Note that I'm running each process as a background process to ensure that I can run each executable simultaneously.