Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   howto get the path of the executable? (http://www.velocityreviews.com/forums/t459790-howto-get-the-path-of-the-executable.html)

patrik.kahari@googlemail.com 01-12-2007 01:21 PM

howto get the path of the executable?
 
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


=?iso-8859-1?q?Erik_Wikstr=F6m?= 01-12-2007 01:45 PM

Re: howto get the path of the executable?
 
On Jan 12, 2:21 pm, patrik.kah...@googlemail.com 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


Zara 01-12-2007 02:26 PM

Re: howto get the path of the executable?
 
On 12 Jan 2007 05:45:10 -0800, "Erik Wikström"
<eriwik@student.chalmers.se> wrote:

>On Jan 12, 2:21 pm, patrik.kah...@googlemail.com 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.

=?ISO-8859-15?Q?Juli=E1n?= Albo 01-12-2007 03:53 PM

Re: howto get the path of the executable?
 
Zara wrote:

>>On Jan 12, 2:21 pm, patrik.kah...@googlemail.com 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-1?Q?Erik_Wikstr=F6m?= 01-12-2007 05:00 PM

Re: howto get the path of the executable?
 
On 2007-01-12 15:26, Zara wrote:
> On 12 Jan 2007 05:45:10 -0800, "Erik Wikström"
> <eriwik@student.chalmers.se> wrote:
>
>>On Jan 12, 2:21 pm, patrik.kah...@googlemail.com 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

Heinz Ozwirk 01-12-2007 07:16 PM

Re: howto get the path of the executable?
 
<patrik.kahari@googlemail.com> schrieb im Newsbeitrag
news:1168608105.231621.187200@v45g2000cwv.googlegr oups.com...
> 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



Alan Johnson 01-13-2007 10:42 PM

Re: howto get the path of the executable?
 
Julián Albo wrote:
> Zara wrote:
>
>>> On Jan 12, 2:21 pm, patrik.kah...@googlemail.com 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? :-(

patrik.kahari@googlemail.com 01-17-2007 12:26 PM

Re: howto get the path of the executable?
 
> 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



All times are GMT. The time now is 06:05 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.