I read through some topic about relative path, but i still got i wrong. I hope sb can help me :). I am using Visual studio 2013, windows 7
I got the following directories:
Here is my .exe file D:\uni\c++\ex5\msvc2013\ex5\Debug
Here is the file i want to read D:\uni\c++\ex5\res\thehead.raw
The code for opening the file:
FILE* f;
f = fopen("..\\..\\..\\res\\thehead.raw", "rb");
if (f == NULL)
printf("FAIL!!");
As i need to use relative paths i figured it out as following: ..\ gets to parent directory.
so "..\..\..\" should get me into the folder "D:\uni\c++\ex5\".
\res should open the res foulder.
Needless to say it fails and i have no idea why. Any help would be appreciated.
Relative paths are relative to the current working directory, not the path of the executable. The current working directory is the directory from which you started the program.
To treat a path as relative to the position of the executable, the simplest portable option is to access the executable as argv[0]
, extract the directory, and chdir()
into it. Note that this will work only as long as the program was itself started with the full path name.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With