Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Finding a program's directory at runtime

Reply
Thread Tools

Finding a program's directory at runtime

 
 
Jay
Guest
Posts: n/a
 
      12-21-2005
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

 
Reply With Quote
 
 
 
 
Artie Gold
Guest
Posts: n/a
 
      12-21-2005
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!"
 
Reply With Quote
 
 
 
 
mlimber
Guest
Posts: n/a
 
      12-21-2005
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

 
Reply With Quote
 
Mike Wahler
Guest
Posts: n/a
 
      12-21-2005

"Jay" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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


 
Reply With Quote
 
Marcus Kwok
Guest
Posts: n/a
 
      12-21-2005
Mike Wahler <(E-Mail Removed)> wrote:
>
> "Jay" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) 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
 
Reply With Quote
 
Mike Wahler
Guest
Posts: n/a
 
      12-22-2005

"Marcus Kwok" <(E-Mail Removed)> wrote in message
news:dochd1$ki3$(E-Mail Removed)...
> Mike Wahler <(E-Mail Removed)> wrote:
>>
>> "Jay" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed) 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
System.IO.Directory.GetDirectories() and System.IO.Directory.GetFiles() are not returning the specified directory Nathan Sokalski ASP .Net 2 09-06-2007 03:58 PM
Runtime.exec() not finding certain programs. Dave Rudolf Java 4 01-17-2007 02:48 PM
Finding memory of a Datatable at runtime.... conckrish@gmail.com ASP .Net 2 04-19-2006 04:51 AM
Finding Server... Finding Host.... enough already!!! Leesa_Tay@softhome.net Computer Support 2 01-20-2006 10:23 AM
ASP.Net Runtime Hosting and finding Assemblies Rick Strahl [MVP] ASP .Net 5 11-02-2003 03:33 PM



Advertisments