473,698 Members | 2,134 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Jan 12 '07 #1
7 24767
On Jan 12, 2:21 pm, patrik.kah...@g ooglemail.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

Jan 12 '07 #2
On 12 Jan 2007 05:45:10 -0800, "Erik Wikström"
<er****@student .chalmers.sewro te:
>On Jan 12, 2:21 pm, patrik.kah...@g ooglemail.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.
Jan 12 '07 #3
Zara wrote:
>>On Jan 12, 2:21 pm, patrik.kah...@g ooglemail.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
Jan 12 '07 #4
On 2007-01-12 15:26, Zara wrote:
On 12 Jan 2007 05:45:10 -0800, "Erik Wikström"
<er****@student .chalmers.sewro te:
>>On Jan 12, 2:21 pm, patrik.kah...@g ooglemail.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
Jan 12 '07 #5
<pa***********@ googlemail.coms chrieb im Newsbeitrag
news:11******** **************@ v45g2000cwv.goo glegroups.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
Jan 12 '07 #6
Julián Albo wrote:
Zara wrote:
>>On Jan 12, 2:21 pm, patrik.kah...@g ooglemail.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? :-(
Jan 13 '07 #7
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 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
1576
by: Steven Bethard | last post by:
This has probably been answered before, but my Google skills have failed me so far... Is there an os independent way of checking to see if a particular executable is on the path? Basically what I want to do is run code like: i, o, e = os.popen3(executable_name) but I'd like to give an informative error if 'executable_name' doesn't refer to an executable on the path. The idea is to differentiate between errors generated by not being...
4
2473
by: presentt | last post by:
Hello, I'm running Ubuntu Linux 5.04. I just started teaching myself Python today, and have been reading a few things to get started. I came across something in one (namely http://docs.python.org/tut/node4.html#SECTION004220000000000000000) that confused me a little. It says:
4
2848
by: Ray Dukes | last post by:
What I am looking to do is map the implementation of interface properties and functions to an inherited method of the base class. Please see below. '**************************************************************************** ' Issues '****************************************************************************
8
4188
by: Topper | last post by:
Hello. I have simple web folders structure: -ROOT - BIN WebService.dll WebService.asmx I need to use my WebService.dll not in bin folder - for example, in ROOT. How do i this? How can i do this without .config file only with some configuration
1
2945
by: Marcel Brekelmans | last post by:
Hello, I know about the FileInfo.LastWriteTime() function. However, this is not always returning the modified date of the executable that I want: when you copy the executable to another directory the FileInfo.LastWriteTime() function returns the copy-time. I can see in my explorer that there is another modified time, the time when I compiled the executable. My explorer (explorer2, that is, Zabkat's dual-pane explorer) shows this...
28
20614
by: Tim Daneliuk | last post by:
I have a program wherein I want one behavior when a file is set as executable and a different behavior if it is not. Is there a simple way to determine whether a given named file is executable that does not resort to all the lowlevel ugliness of os.stat() AND that is portable across Win32 and *nix? Thanks, ---------------------------------------------------------------------------- Tim Daneliuk tundra@tundraware.com PGP Key: ...
34
3957
by: Ben Sizer | last post by:
I've installed several different versions of Python across several different versions of MS Windows, and not a single time was the Python directory or the Scripts subdirectory added to the PATH environment variable. Every time, I've had to go through and add this by hand, to have something resembling a usable Python installation. No such problems on Linux, whether it be Mandrake/Mandriva, Fedora Core, or Kubuntu. So why is the Windows...
2
1826
by: NasirMunir | last post by:
I am trying to run an executable using shell. The executable look for certain files in the same directory and then run on those files. My problem: I have created a form which asks from the user to locate those files using browse option. Then I ask the user to locate the executable. The next step is to copy the executable to those files directory. Everything is dandy uptil here, but when I use the shell to run that executable, the executable...
9
4934
by: mathieu | last post by:
I'd like to be able to get the path to the current working executable (from inside it). Technically this is easy, I simply have to collapse: getcwd and argv Well argv comes in a little late, I'd like to have access to this information before the 'main' function is called. Is there a way to get the path to an executable (from inside it) ?
0
8674
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9157
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9028
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8895
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7728
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6518
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5860
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4369
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4619
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.