Finding a program's directory at runtime

J

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
 
A

Artie Gold

Jay said:
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
 
M

Mike Wahler

Jay said:
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
 
M

Marcus Kwok

Mike Wahler said:
Jay said:
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
 
M

Mike Wahler

Marcus Kwok said:
Mike Wahler said:
Jay said:
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
 

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,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top