473,385 Members | 1,536 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Finding a program's directory at runtime

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
5 1893
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
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

"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
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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Kevin Thorpe | last post by:
I don't suppose anyone knows of a script/program to try and identify where variables are used assuming register_globals is on? I'm trying to fix an application and would rather not turn it on as...
2
by: Mike | last post by:
I am sure that I am making a simple boneheaded mistake and I would appreciate your help in spotting in. I have just installed apache_2.0.53-win32-x86-no_ssl.exe php-5.0.3-Win32.zip...
4
by: Hal Vaughan | last post by:
I want to have a config file for my program, which means I need to know where the config file is. If I type: java myclass and it runs myclass.class, is there any way to obtain the location of...
1
by: Todd Johnson | last post by:
Hey, I have been working on a program that stores information for each user in a subdirectory of the directory in which the script is run. So if my script is in /home/someuser/myprogram, the...
8
by: Rick Strahl [MVP] | last post by:
Hi all, I'm building an app that uses the ASP.Net runtime... One problem I've run into is that pages running inside of the runtime are not finding DLLs in the GAC. In fact, if I look at the...
6
by: Sigmathaar | last post by:
Hi, while making an XML parser for the creation of an entire directory list I got this problem and I'm having trouble solving it. The program (not yet written) goes through an XML Schema Instance...
2
by: Rich | last post by:
I am trying to use unmanaged DLLs within managed code. My approach is to create a managed DLL with wrapper functions for my unmanaged functions. The unmanaged functions come from an unmanaged...
8
by: Joshua J. Kugler | last post by:
So, I have: ModTest __init__.py AModule.py BModule.py CModule.py All works fine. However, when I import ModTest, I would like it to discover and store the names of the modules beneath it,...
1
Nepomuk
by: Nepomuk | last post by:
Hi! I'm trying to run an external Program with Process p = Runtime.getRuntime().exec("/bin/sh -c \"/bin/gzip -c /home/user/workspace/TarGz/pics.tar > pics.tar.gz\""); CleanStream cleanError =...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.