Finding a program's directory at runtime

Discussion in 'C++' started by Jay, Dec 21, 2005.

  1. Jay

    Jay Guest

    Hello,
    I am writing a c++ program that stores some values in a config located
    in it's directory.
    is there a way in c++ to get the full path of the running program so I
    can open the file not relative to where I run it from but where it is
    located?

    Thanks
    Jay
    Jay, Dec 21, 2005
    #1
    1. Advertising

  2. Jay

    Artie Gold Guest

    Jay wrote:
    > Hello,
    > I am writing a c++ program that stores some values in a config located
    > in it's directory.
    > is there a way in c++ to get the full path of the running program so I
    > can open the file not relative to where I run it from but where it is
    > located?
    >
    > Thanks
    > Jay
    >

    The C++ language knows nothing of such things; it's inherently platform
    specific -- and sometimes not even (reliably) possible.

    Your best bet is to try a platform specific forum or find another way to
    solve the problem.

    Sorry, but HTH,
    --ag

    --
    Artie Gold -- Austin, Texas
    http://goldsays.blogspot.com (new post 8/5)
    http://www.cafepress.com/goldsays
    "If you have nothing to hide, you're not trying!"
    Artie Gold, Dec 21, 2005
    #2
    1. Advertising

  3. Jay

    mlimber Guest

    Jay wrote:
    > Hello,
    > I am writing a c++ program that stores some values in a config located
    > in it's directory.
    > is there a way in c++ to get the full path of the running program so I
    > can open the file not relative to where I run it from but where it is
    > located?
    >
    > Thanks
    > Jay


    You might be able to use the Boost Filesystem library:

    http://boost.org/libs/filesystem/doc/index.htm

    Cheers! --M
    mlimber, Dec 21, 2005
    #3
  4. Jay

    Mike Wahler Guest

    "Jay" <> wrote in message
    news:...
    > Hello,
    > I am writing a c++ program that stores some values in a config located
    > in it's directory.
    > is there a way in c++ to get the full path of the running program so I
    > can open the file not relative to where I run it from but where it is
    > located?


    Some implementations, e.g. for Windows, provide the full
    path of the executable as main()'s argv[0]. But this is
    not guaranteed by the language.

    -Mike
    Mike Wahler, Dec 21, 2005
    #4
  5. Jay

    Marcus Kwok Guest

    Mike Wahler <> wrote:
    >
    > "Jay" <> wrote in message
    > news:...
    >> Hello,
    >> I am writing a c++ program that stores some values in a config located
    >> in it's directory.
    >> is there a way in c++ to get the full path of the running program so I
    >> can open the file not relative to where I run it from but where it is
    >> located?

    >
    > Some implementations, e.g. for Windows, provide the full
    > path of the executable as main()'s argv[0]. But this is
    > not guaranteed by the language.


    True that it is not guaranteed by the language. However, I think your
    wording may be a little misleading: when I first read it, I interpreted
    your statement as meaning that all implementations on Windows will
    provide the full executable path. For example,


    #include <iostream>

    int main(int argc, char* argv[])
    {
    std::cout << "I am: " << argv[0] << '\n';

    return 0;
    }


    only prints whatever I used to invoke the program. If I am in the same
    directory as the program, I get:

    I am: test

    and if I move one directory down in the hierarchy but invoke the program
    in the parent directory, I get:

    I am: ..\test

    --
    Marcus Kwok
    Marcus Kwok, Dec 21, 2005
    #5
  6. Jay

    Mike Wahler Guest

    "Marcus Kwok" <> wrote in message
    news:dochd1$ki3$...
    > Mike Wahler <> wrote:
    >>
    >> "Jay" <> wrote in message
    >> news:...
    >>> Hello,
    >>> I am writing a c++ program that stores some values in a config located
    >>> in it's directory.
    >>> is there a way in c++ to get the full path of the running program so I
    >>> can open the file not relative to where I run it from but where it is
    >>> located?

    >>
    >> Some implementations, e.g. for Windows, provide the full
    >> path of the executable as main()'s argv[0]. But this is
    >> not guaranteed by the language.

    >
    > True that it is not guaranteed by the language. However, I think your
    > wording may be a little misleading: when I first read it, I interpreted
    > your statement as meaning that all implementations on Windows


    See above, "Some implementations"

    >will
    > provide the full executable path. For example,
    >
    >
    > #include <iostream>
    >
    > int main(int argc, char* argv[])
    > {
    > std::cout << "I am: " << argv[0] << '\n';
    >
    > return 0;
    > }
    >
    >
    > only prints whatever I used to invoke the program. If I am in the same
    > directory as the program, I get:
    >
    > I am: test
    >
    > and if I move one directory down in the hierarchy but invoke the program
    > in the parent directory, I get:
    >
    > I am: ..\test


    This all depends upon the compiler and platform. Sorry if I wasn't
    clear.


    -Mike
    Mike Wahler, Dec 22, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rick Strahl [MVP]

    ASP.Net Runtime Hosting and finding Assemblies

    Rick Strahl [MVP], Oct 18, 2003, in forum: ASP .Net
    Replies:
    5
    Views:
    667
    Natty Gur
    Nov 2, 2003
  2. Replies:
    2
    Views:
    426
  3. Dave Rudolf
    Replies:
    4
    Views:
    5,664
    Dave Rudolf
    Jan 17, 2007
  4. Joshua J. Kugler

    Finding a module's sub modules at runtime

    Joshua J. Kugler, Mar 28, 2007, in forum: Python
    Replies:
    8
    Views:
    371
    Joshua J. Kugler
    Apr 3, 2007
  5. Jürgen Exner
    Replies:
    0
    Views:
    3,251
    Jürgen Exner
    Oct 13, 2009
Loading...

Share This Page