Finding the executing C++ program location (win32)

Y

yzz

Hi,
How can I find the directory the C++ exe file is running from
e.g. If I stand in C: and I execute c:\foo\foo.exe the program output
will be c:\foo

Thanks
 
C

cloud

Hi,
How can I find the directory the C++ exe file is running from
e.g. If I stand in C: and I execute c:\foo\foo.exe the program output
will be c:\foo

Thanks

Hi,

'CurrentDir' - is the function which will return the current directory
in which the program stands. Also there is one more method to find the
path, please refer K&R C for exact function names.

- vinod
 
Z

Zephryn Xirdal

Hi,
How can I find the directory the C++ exe file is running from
e.g. If I stand in C: and I execute c:\foo\foo.exe the program output
will be c:\foo

Thanks

Fist string array passed to main is the program name. Then you've to
substring to last '\' delimiter and you've got it.

int main(int argv,char *argc[]);

argc[0] -> Is exe's path as null terminated strign.

--
Visita mi blog principal: http://rfog.blogsome.com
Y este sobre programación: http://geeks.ms/blogs/rfog
Libros, ciencia ficción y programación
========================================
Sólo en la filosofía es donde cada pensador, cuando es original, determina
no únicamente lo que quiere responder, sino lo que quiere preguntar...
para responder al concepto de filosofía.
-- Georg Simmel. (1858-1918) Filósofo alemán.
 
?

=?iso-8859-1?q?Erik_Wikstr=F6m?=

Hi,
How can I find the directory the C++ exe file is running from
e.g. If I stand in C: and I execute c:\foo\foo.exe the program output
will be c:\foo

Fist string array passed to main is the program name. Then you've to
substring to last '\' delimiter and you've got it.

int main(int argv,char *argc[]);

argc[0] -> Is exe's path as null terminated strign.

Yes, but there's no guarantee that the actual path is included, just
the name the file was called with.
 
Z

Zephryn Xirdal

En Thu, 12 Apr 2007 11:48:00 +0200, Erik Wikström
Hi,
How can I find the directory the C++ exe file is running from
e.g. If I stand in C: and I execute c:\foo\foo.exe the program output
will be c:\foo

Fist string array passed to main is the program name. Then you've to
substring to last '\' delimiter and you've got it.

int main(int argv,char *argc[]);

argc[0] -> Is exe's path as null terminated strign.

Yes, but there's no guarantee that the actual path is included, just
the name the file was called with.

Then use GetCommandLine win32 API function. SDK says: "The operating
system may prepend a fully qualified path to an executable name that is
provided without a fully qualified path"

--
Visita mi blog principal: http://rfog.blogsome.com
Y este sobre programación: http://geeks.ms/blogs/rfog
Libros, ciencia ficción y programación
========================================
Sólo en la filosofía es donde cada pensador, cuando es original, determina
no únicamente lo que quiere responder, sino lo que quiere preguntar...
para responder al concepto de filosofía.
-- Georg Simmel. (1858-1918) Filósofo alemán.
 
G

Gianni Mariani

yzz said:
Hi,
How can I find the directory the C++ exe file is running from
e.g. If I stand in C: and I execute c:\foo\foo.exe the program output
will be c:\foo

Thanks

There is no C++ standard way to do this and so totally off topic here !

In win32, the best way is to use:

char l_name[ MAX_PATH + 1 ];
l_name[ sizeof( l_name ) -1] = 0;
::GetModuleFileName( NULL, l_name, sizeof( l_name ) -1 );

In unix, the not 100% reliable but mostly good way is to take argv[0]
and if the name does not start with "/" then get the working directory
and slap it in front.


if ( argv[ 0 ][ 0 ] == '/' )
{
at::FilePath l_filepath( argv[ 0 ] );
return l_filepath.Head().StlString();
}

at::FilePath l_current_dir = at::CurrentDirectory();
return (
l_current_dir / std::string( argv[ 0 ] )
).Head().Clean().StlString();

(with the austria C++ file path stuff in the alpha
http://netcabletv.org/public_releases/)
 
M

Miguel Gimenez

Zephryn said:
En Thu, 12 Apr 2007 11:48:00 +0200, Erik Wikström
En Thu, 12 Apr 2007 10:48:29 +0200, yzz <[email protected]> escribió:

Hi,
How can I find the directory the C++ exe file is running from
e.g. If I stand in C: and I execute c:\foo\foo.exe the program output
will be c:\foo

Thanks

Fist string array passed to main is the program name. Then you've to
substring to last '\' delimiter and you've got it.

int main(int argv,char *argc[]);

argc[0] -> Is exe's path as null terminated strign.

Yes, but there's no guarantee that the actual path is included, just
the name the file was called with.

Then use GetCommandLine win32 API function. SDK says: "The operating
system may prepend a fully qualified path to an executable name that is
provided without a fully qualified path"

"May" is different from "Must". You have no guarantee, again.

P.S. Me gusta tu blog
 
Z

Zeppe

yzz said:
Hi,
How can I find the directory the C++ exe file is running from
e.g. If I stand in C: and I execute c:\foo\foo.exe the program output
will be c:\foo

Short answer: you can't in standard C++, so you are off topic.

A little bit longer answer: you can't in standard C++, however there are
some nice libraries called "boost", some of those will probably inserted
on the next C++ standard. One of them, that probably is not yet in but
is in the Stroustrup wish list ( not the first guy around :) ) is the
filesystem library. Look at:
http://www.boost.org/libs/filesystem/doc/operations.htm#current_path

probably this is the most portable and close to the standard way to do
what you are asking.

Regards,

Zeppe
 

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,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top