howto get the path of the executable?

Discussion in 'C++' started by patrik.kahari@googlemail.com, Jan 12, 2007.

  1. Guest

    Is there a c++ function similar to getcwd that does not give you the
    working directory but the directory or path of the currently running
    executable?

    I need this because im porting a game that uses relative paths to
    images. This works fine when the game is executed directly from the
    directory its in (as in ./game &). It does not work when its executed
    from another location (as in /root/games/game &)

    Regards Patrik
     
    , Jan 12, 2007
    #1
    1. Advertising

  2. On Jan 12, 2:21 pm, wrote:
    > Is there a c++ function similar to getcwd that does not give you the
    > working directory but the directory or path of the currently running
    > executable?
    >
    > I need this because im porting a game that uses relative paths to
    > images. This works fine when the game is executed directly from the
    > directory its in (as in ./game &). It does not work when its executed
    > from another location (as in /root/games/game &)


    It's the first argument to the program.

    #include <iostream>

    int main(int argc, char* argv[])
    {
    std::cout << argv[0] << std::endl;
    return 0;
    }

    --
    Erik Wikström
     
    =?iso-8859-1?q?Erik_Wikstr=F6m?=, Jan 12, 2007
    #2
    1. Advertising

  3. Zara Guest

    On 12 Jan 2007 05:45:10 -0800, "Erik Wikström"
    <> wrote:

    >On Jan 12, 2:21 pm, wrote:
    >> Is there a c++ function similar to getcwd that does not give you the
    >> working directory but the directory or path of the currently running
    >> executable?
    >>
    >> I need this because im porting a game that uses relative paths to
    >> images. This works fine when the game is executed directly from the
    >> directory its in (as in ./game &). It does not work when its executed
    >> from another location (as in /root/games/game &)

    >
    >It's the first argument to the program.
    >
    >#include <iostream>
    >
    >int main(int argc, char* argv[])
    >{
    > std::cout << argv[0] << std::endl;
    > return 0;
    >}



    Remeber: This is the usual way, but the standard does not guarantee
    it.
     
    Zara, Jan 12, 2007
    #3
  4. Zara wrote:

    >>On Jan 12, 2:21 pm, wrote:
    >>> Is there a c++ function similar to getcwd that does not give you the
    >>> working directory but the directory or path of the currently running
    >>> executable?

    (snip)
    >>It's the first argument to the program.

    (snip)
    > Remeber: This is the usual way, but the standard does not guarantee it.


    For example, it does not work in ms-dos earlier than 3.0 because the
    operating system does not provide that information... but I don't konw if
    there are standard compliant C++ compilers that create programs able to run
    in that versions.

    --
    Salu2
     
    =?ISO-8859-15?Q?Juli=E1n?= Albo, Jan 12, 2007
    #4
  5. On 2007-01-12 15:26, Zara wrote:
    > On 12 Jan 2007 05:45:10 -0800, "Erik Wikström"
    > <> wrote:
    >
    >>On Jan 12, 2:21 pm, wrote:
    >>> Is there a c++ function similar to getcwd that does not give you the
    >>> working directory but the directory or path of the currently running
    >>> executable?
    >>>
    >>> I need this because im porting a game that uses relative paths to
    >>> images. This works fine when the game is executed directly from the
    >>> directory its in (as in ./game &). It does not work when its executed
    >>> from another location (as in /root/games/game &)

    >>
    >>It's the first argument to the program.
    >>
    >>#include <iostream>
    >>
    >>int main(int argc, char* argv[])
    >>{
    >> std::cout << argv[0] << std::endl;
    >> return 0;
    >>}

    >
    >
    > Remeber: This is the usual way, but the standard does not guarantee
    > it.


    Yes, there is no guarantee that argv[0] contains anything (except 0),
    you should always check so that argc > 0 before accessing argv.

    On most modern platform it's a good bet that argv[0] is set, however it
    is just as likely to contain a relative path as the full path.

    --
    Erik Wikström
     
    =?ISO-8859-1?Q?Erik_Wikstr=F6m?=, Jan 12, 2007
    #5
  6. Heinz Ozwirk Guest

    <> schrieb im Newsbeitrag
    news:...
    > Is there a c++ function similar to getcwd that does not give you the
    > working directory but the directory or path of the currently running
    > executable?
    >
    > I need this because im porting a game that uses relative paths to
    > images. This works fine when the game is executed directly from the
    > directory its in (as in ./game &). It does not work when its executed
    > from another location (as in /root/games/game &)


    There is no portable way to determine the full name of the executable, and
    even if it were, it would be of limited use. A non-system program, including
    or especially games, should not be able to write to the directory where
    programs are stored. Programs should write there data to the current user's
    home directory (or a subdirectory of it), but usually not to a system
    directory, neither to /root nor to c:\winnt or whatever.

    Let the program write to the current directory, that's probably why it does
    use relative paths, or let the user specify a working directory on the
    command line, or use an environment variable to specify that directory, but
    don't assume that you can write to the directory, where the executable is
    stored on disk. Imagine what would happen if you run the program from
    CD-ROM, not to mention what might happen if every stupid program could
    create files everywhere in the system.

    HTH
    heinz
     
    Heinz Ozwirk, Jan 12, 2007
    #6
  7. Alan Johnson Guest

    Julián Albo wrote:
    > Zara wrote:
    >
    >>> On Jan 12, 2:21 pm, wrote:
    >>>> Is there a c++ function similar to getcwd that does not give you the
    >>>> working directory but the directory or path of the currently running
    >>>> executable?

    > (snip)
    >>> It's the first argument to the program.

    > (snip)
    >> Remeber: This is the usual way, but the standard does not guarantee it.

    >
    > For example, it does not work in ms-dos earlier than 3.0 because the
    > operating system does not provide that information... but I don't konw if
    > there are standard compliant C++ compilers that create programs able to run
    > in that versions.
    >


    Does that mean we'll never get a MS-DOS 3.0 port of Patrik's game? :-(
     
    Alan Johnson, Jan 13, 2007
    #7
  8. Guest

    > Imagine what would happen if you run the program from
    > CD-ROM, not to mention what might happen if every stupid program could
    > create files everywhere in the system.


    Thanks all for the comments. I was in such a hurry I didnt get a chance
    to read the comments before now.

    I found the same argv[0] trick you mentioned. I ended up used argv[0]
    together with the getcwd and some string manipulation to figure out the
    directory of the executable. I understand the format of this string is
    not specified in the standard. It however works fine on the specific
    platforms i need to port for.

    The game writes to nad reads from the same folder as the execute is in.
    This seemed like the natural thing to me. That is to keep data that are
    owned by the same application together in the same place instead of
    scattering it around the system.

    But you do bring up an important point. I shouldnt have assumed I have
    write permissions to this directory the executable is in.

    So if the read/execute folder can be assumed to be the same but the
    write folder cant then i need a way to configure the different write
    path. I have my read / execute path and to get the write path, i could:

    A Hardcoding the write path.

    B get the working directory. I cant assume the working directory is the
    write directory because of integration issues (the script that launches
    the game, launches it from root so root will be working directory.
    Since I dont own the platform so i cant change the way the script
    works.)

    C Game specific enviornment environment variable. Reading some game
    specific enviornment variable wont do eighter, because again I do not
    own the platform and am not free to install my own game specific
    environment variables.

    D Using user's home directory. Finding out the current user's home
    directory from c++ does not sound very portable eighter. Again id
    probably have to read some platform specific enviornment variable.

    E Reading the write path from a file. Having a file called
    writePath.txt in the same folder as the executable.

    I im going to go with the last option.

    Regards Patrik
     
    , Jan 17, 2007
    #8
    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. Abraham Andres Luna

    howto launch an executable from an aspx page

    Abraham Andres Luna, Feb 17, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    2,183
    Abraham Andres Luna
    Feb 20, 2006
  2. Replies:
    15
    Views:
    498
    Dave Thompson
    Aug 21, 2006
  3. pratap
    Replies:
    20
    Views:
    774
    Flash Gordon
    Mar 7, 2007
  4. Jason Cavett
    Replies:
    8
    Views:
    634
    Sanjay
    May 16, 2007
  5. Replies:
    0
    Views:
    449
Loading...

Share This Page