howto get the path of the executable?

P

patrik.kahari

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?=

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;
}
 
Z

Zara

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

Zara said:
(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.
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

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.
 
H

Heinz Ozwirk

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
 
A

Alan Johnson

Julián Albo said:
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? :-(
 
P

patrik.kahari

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top