Thread: Path of file relative to program

  1. #1
    Math wizard
    Join Date
    Dec 2006
    Location
    USA
    Posts
    582

    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. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    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");
    
    ...
    Last edited by nadroj; 04-15-2007 at 01:44 AM.

  3. #3
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    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. #4
    Math wizard
    Join Date
    Dec 2006
    Location
    USA
    Posts
    582
    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:

    LoadFile(".\\Images\\Logo.bmp", other, parameters);

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

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

    char LoadFile(char FileName[96], other, parameters)
    {

    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. #5
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    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. #6
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    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);
    @nadroj - Escape your chars,
    Code:
    relative2 = fopen("images\\myfile.txt","r");
    absolute = fopen("c:\\stuff\\myfile.txt","r");
    Last edited by zacs7; 04-15-2007 at 02:33 AM.

  7. #7
    Math wizard
    Join Date
    Dec 2006
    Location
    USA
    Posts
    582
    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. #8
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    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. #9
    Registered User
    Join Date
    Apr 2007
    Location
    In my house
    Posts
    29
    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. #10
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    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. #11
    Registered User
    Join Date
    Apr 2007
    Location
    In my house
    Posts
    29
    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. #12
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    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. #13
    Registered User
    Join Date
    Apr 2007
    Location
    In my house
    Posts
    29
    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. #14
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by Nurfina View Post
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  2. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  3. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM