By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,851 Members | 2,104 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,851 IT Pros & Developers. It's quick & easy.

Finding a program's directory at runtime

P: n/a
Jay
Hello,
I am writing a c++ program that stores some values in a config located
in it's directory.
is there a way in c++ to get the full path of the running program so I
can open the file not relative to where I run it from but where it is
located?

Thanks
Jay

Dec 21 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Jay wrote:
Hello,
I am writing a c++ program that stores some values in a config located
in it's directory.
is there a way in c++ to get the full path of the running program so I
can open the file not relative to where I run it from but where it is
located?

Thanks
Jay

The C++ language knows nothing of such things; it's inherently platform
specific -- and sometimes not even (reliably) possible.

Your best bet is to try a platform specific forum or find another way to
solve the problem.

Sorry, but HTH,
--ag

--
Artie Gold -- Austin, Texas
http://goldsays.blogspot.com (new post 8/5)
http://www.cafepress.com/goldsays
"If you have nothing to hide, you're not trying!"
Dec 21 '05 #2

P: n/a
Jay wrote:
Hello,
I am writing a c++ program that stores some values in a config located
in it's directory.
is there a way in c++ to get the full path of the running program so I
can open the file not relative to where I run it from but where it is
located?

Thanks
Jay


You might be able to use the Boost Filesystem library:

http://boost.org/libs/filesystem/doc/index.htm

Cheers! --M

Dec 21 '05 #3

P: n/a

"Jay" <Co******@gmail.com> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...
Hello,
I am writing a c++ program that stores some values in a config located
in it's directory.
is there a way in c++ to get the full path of the running program so I
can open the file not relative to where I run it from but where it is
located?


Some implementations, e.g. for Windows, provide the full
path of the executable as main()'s argv[0]. But this is
not guaranteed by the language.

-Mike
Dec 21 '05 #4

P: n/a
Mike Wahler <mk******@mkwahler.net> wrote:

"Jay" <Co******@gmail.com> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...
Hello,
I am writing a c++ program that stores some values in a config located
in it's directory.
is there a way in c++ to get the full path of the running program so I
can open the file not relative to where I run it from but where it is
located?


Some implementations, e.g. for Windows, provide the full
path of the executable as main()'s argv[0]. But this is
not guaranteed by the language.


True that it is not guaranteed by the language. However, I think your
wording may be a little misleading: when I first read it, I interpreted
your statement as meaning that all implementations on Windows will
provide the full executable path. For example,
#include <iostream>

int main(int argc, char* argv[])
{
std::cout << "I am: " << argv[0] << '\n';

return 0;
}
only prints whatever I used to invoke the program. If I am in the same
directory as the program, I get:

I am: test

and if I move one directory down in the hierarchy but invoke the program
in the parent directory, I get:

I am: ..\test

--
Marcus Kwok
Dec 21 '05 #5

P: n/a

"Marcus Kwok" <ri******@gehennom.net> wrote in message
news:do**********@news-int2.gatech.edu...
Mike Wahler <mk******@mkwahler.net> wrote:

"Jay" <Co******@gmail.com> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...
Hello,
I am writing a c++ program that stores some values in a config located
in it's directory.
is there a way in c++ to get the full path of the running program so I
can open the file not relative to where I run it from but where it is
located?
Some implementations, e.g. for Windows, provide the full
path of the executable as main()'s argv[0]. But this is
not guaranteed by the language.


True that it is not guaranteed by the language. However, I think your
wording may be a little misleading: when I first read it, I interpreted
your statement as meaning that all implementations on Windows


See above, "Some implementations"
will
provide the full executable path. For example,
#include <iostream>

int main(int argc, char* argv[])
{
std::cout << "I am: " << argv[0] << '\n';

return 0;
}
only prints whatever I used to invoke the program. If I am in the same
directory as the program, I get:

I am: test

and if I move one directory down in the hierarchy but invoke the program
in the parent directory, I get:

I am: ..\test


This all depends upon the compiler and platform. Sorry if I wasn't
clear.
-Mike
Dec 22 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.