# Path of file relative to program

This is a discussion on Path of file relative to program within the C Programming forums, part of the General Programming Boards category; When loading a file, a path to it is specified like this: C:\Path\to\file\Filename.ext How do I get it so that ...

1. ## Path of file relative to program

When loading a file, a path to it is specified like this:

C:\\Path\\to\\file\\Filename.ext

How do I get it so that the file being loaded is relative to the path of the program using it, rather than an absolute path? That is, if the program was at:

C:\\My Documents\\My programs\\Test program.exe

How would I load a file from this:

C:\\My Documents\\My programs\\Images\\example.bmp

without having to specify the "C:\\My Documents\\My programs\\" part, or, in other words, relative to the program's location?

2. Code:
FILE* relative1;
FILE* relative2;
FILE* absolute;

relative1 = fopen("myfile.txt","r");
relative2 = fopen("images\myfile.txt","r");
absolute = fopen("c:\stuff\myfile.txt","r");

...

3. well, it'd be

.\\Images\\example.bmp

. Specifing the current directory (usually not needed). So Technically

Images\\example.bmp is also valid.

Or you can use GetCurrentDirectory() to get the current working directory and make a full path...

4. I've been given 4 different variations, but none of which are working. The executable is here:

C:\My Documents\My programs\Interactive Animation\debug\Interactive Animation.exe

The image I'm trying to load (well, one of them anyway) is here:

C:\My Documents\My programs\Interactive Animation\debug\Images\Logo.bmp

The function used to load files is this:

Only the relevant parameter is given, the others just set pointers.

This is the function and it's parameters (the one in focus):

{

And this is what is used to load the file:

FileHandle = fopen(FileName, "rb");

Using absolute paths, it works, but relative paths aren't. What's wrong here?

5. Doublecheck to see if the program is really working inside the directory that it is in, and that its current directory is not some other one.

6. If you run your program through the VS (judging from your screenshot in the last thread), then the working directory isn't set to where the program resides (well on VS6 anyway).

May I suggest a revamp of LoadFile?

Code:
char LoadFile(const char * FileName, ...
Doublecheck to see if the program is really working inside the directory that it is in, and that its current directory is not some other one.
Yes, see the VS comment ^^.

Test with:

Code:
char path[MAX_PATH];
GetCurrentDirectory(sizeof(path), path);
MessageBox(0, path, "Current Path", 0);
Code:
relative2 = fopen("images\\myfile.txt","r");
absolute = fopen("c:\\stuff\\myfile.txt","r");

7. What do you mean by "working directory"? What is VS6 also? Visual Studio 6? I have Visual C++ 2005 Express.

What I'm after is mainly just having the file be read from a path relative to the EXE. I use the "test run" button in Visual C++ 2005 Express to run my program rather than using the EXE.

8. Working directory is the programs root directory (by default - where the program resides), It can be changed with SetCurrentDirectory().

If you're using the 'test run' button in VC2006 then the working directory will most likely not be set to where the program is... Compile it then run the program directly, it should now work.

And yes, VS6 is Visual Studio 6.

9. In my experience with VS05, to load a file that is in the same directory as the application code you use "image/example.bmp"

for example :

Code:
fopen("image/example.bmp", "rb");

10. As been pointed out a relative path is always relative to the current working directory and not to the location of the executable. So if you store your resources relative to the location of the executable you have to have a means to find out where your executable is stored.

One way to get that location is to check the commandline parameters. argv[0] is usually the name of the executable. But I think this is not always the full path so this might not work in all cases.

I'm not very familiar with WINAPI but I think GetModuleFileName() would give you the path to the executable. You would just have to remove the filename from that path and append the relative path to the resources.
Kurt

11. with the use of "image/example.bmp" in Windows, as long as the images folder is in the same directory as the .exe the code should work fine . . . or at least whenever I do it it does >.<

12. I agree when you run your app from the ide or from a commandline ( with current working directory set to the path of the .exe ) this works fine.
If you install your app and put the path to the .exe into the PATH environment variable ( to be able to run it from anywhere ), your app will not find its resources anymore.
Kurt

13. hmmm yea, I have yet to do installations of programs yet so I can't help there, but I would asume that if you program the setup file to copy the image folder to the directory of the install my method would still work fine, but I have no way of proving this or providing any kind of setup file code to help

14. Originally Posted by Nurfina
hmmm yea, I have yet to do installations of programs yet so I can't help there, but I would asume that if you program the setup file to copy the image folder to the directory of the install my method would still work fine, but I have no way of proving this or providing any kind of setup file code to help
No. That won't work. The problem is that relative filepath are relative to the current working directory and not relative to the .exe's path.
You can easily test that. Open a command window, change to a directory that is not the path where the exe is and run the program by specifiying the full path to the .exe.
Kurt