473,668 Members | 2,416 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Need help with relative file path

Hey all,

I'm writing a c++ program that needs to read a file. I'm trying to read
a file that is in the same directory as the executable.

Everything works fine if I execute the program while in the program's
directory. What I need to do is read that file regardless of where
(cwd) I execute the program from, without hardcoding the absolute since
the program might be moved or be in differant locations on other
computers.

What should I do?

PS. I'm using the boost filesystem framework if it helps.
Jun 27 '08 #1
12 8585
On Jun 21, 8:11*am, Thomas Børlum <bor...@gmail.c omwrote:
Hey all,

I'm writing a c++ program that needs to read a file. I'm trying to read
a file that is in the same directory as the executable.

Everything works fine if I execute the program while in the program's
directory. What I need to do is read that file regardless of where
(cwd) I execute the program from, without hardcoding the absolute since
the program might be moved or be in differant locations on other
computers.

What should I do?

PS. I'm using the boost filesystem framework if it helps.
Most OS's will give you the command used to execute your program in
argv[0]
This will usually include the path

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

NB. It may be absolute or relative.
Jun 27 '08 #2
Martin York wrote:
Most OS's will give you the command used to execute your program in
argv[0]
This will usually include the path
Actually it's rather usual that argv[0] is simply what you wrote in
the command line as the program's name, without any additions.
Jun 27 '08 #3
On 2008-06-21 17:50:33 +0200, Martin York <Ma************ ***@gmail.comsa id:
On Jun 21, 8:11*am, Thomas Børlum <bor...@gmail.c omwrote:
>Hey all,

I'm writing a c++ program that needs to read a file. I'm trying to read
a file that is in the same directory as the executable.

Everything works fine if I execute the program while in the program's
directory. What I need to do is read that file regardless of where
(cwd) I execute the program from, without hardcoding the absolute since
the program might be moved or be in differant locations on other
computers.

What should I do?

PS. I'm using the boost filesystem framework if it helps.

Most OS's will give you the command used to execute your program in
argv[0]
This will usually include the path

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

NB. It may be absolute or relative.
Thanks that got me on the right track. I've done the following:

path program_path(st ring(argv[0]) + "/..");
program_path = complete(progra m_path);
string settings_file = program_path.st ring() + "/settings.txt";

works great.
Jun 27 '08 #4
On Jun 21, 5:50 pm, Martin York <Martin.YorkAma ...@gmail.comwr ote:
On Jun 21, 8:11 am, Thomas Børlum <bor...@gmail.c omwrote:
I'm writing a c++ program that needs to read a file. I'm
trying to read a file that is in the same directory as the
executable.
Everything works fine if I execute the program while in the
program's directory. What I need to do is read that file
regardless of where (cwd) I execute the program from,
without hardcoding the absolute since the program might be
moved or be in differant locations on other computers.
What should I do?
PS. I'm using the boost filesystem framework if it helps.
Most OS's will give you the command used to execute your
program in argv[0] This will usually include the path
First, of course: I'm not sure what you mean by "most OS's", but
Unix certainly doesn't, and IIRC, nor does Windows. Unix, at
any rate, gives you whatever the invoking program decides.
(Note that this is NOT conform to the C or C++ standards;
strictly speaking, a conforming implementation of C or C++ is
impossible under Unix, and I'm pretty sure under Windows as
well.)

Secondly, of course, it's quite exceptional, both under Unix and
under Windows, for the invoking command to include the path.
In these two systems, the actual path is normally obtained from
an environment variable.
int main(int argc,char* argv[])
{
std::cout << argv[0] << "\n\n";
}
NB. It may be absolute or relative.
Most of the time, it's relative from some arbitrary entry in the
PATH environment variable.

I've encountered this problem several times in the past; at
least under Unix, there is no possible solution from within the
program.

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jun 27 '08 #5
On Jun 21, 6:21 pm, Juha Nieminen <nos...@thanks. invalidwrote:
Martin York wrote:
Most OS's will give you the command used to execute your program in
argv[0]
This will usually include the path
Actually it's rather usual that argv[0] is simply what you
wrote in the command line as the program's name, without any
additions.
That's more or less what the standard requires. Under Unix, of
course, this only works if the shell used to start the command
collaborates (most do). And it leaves open the question as to
what should be in argv[0] if the command is not started from the
command line, but from some other program.

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jun 27 '08 #6
Thomas Børlum <bo****@gmail.c omkirjutas:
Hey all,

I'm writing a c++ program that needs to read a file. I'm trying to read
a file that is in the same directory as the executable.

Everything works fine if I execute the program while in the program's
directory. What I need to do is read that file regardless of where
(cwd) I execute the program from, without hardcoding the absolute since
the program might be moved or be in differant locations on other
computers.

What should I do?

PS. I'm using the boost filesystem framework if it helps.
On Windows, getting the directory where the executable file is located is
easy (GetModuleFileN ame(NULL, ...)). On Linux/Unix it's much harder, for
starters the problem is not uniquely defined (you can have hardlinks to the
same inode in different directories). One can mimick it to some extent by
using argv[0], cwd() and which/PATH, but this requires collaboration by the
caller, as already mentioned by other responders.

Paavo
Jun 27 '08 #7
James Kanze wrote:
On Jun 21, 6:21 pm, Juha Nieminen <nos...@thanks. invalidwrote:
>Martin York wrote:
>>Most OS's will give you the command used to execute your program in
argv[0]
This will usually include the path
>Actually it's rather usual that argv[0] is simply what you
wrote in the command line as the program's name, without any
additions.

That's more or less what the standard requires. Under Unix, of
course, this only works if the shell used to start the command
collaborates (most do). And it leaves open the question as to
what should be in argv[0] if the command is not started from the
command line, but from some other program.
Also, if I'm not mistaken, the C standard doesn't actually guarantee
that argv[0] will contain anything at all (although I don't remember if
that means that argv[0] could actually be a null pointer, or if it
simply means that it points to an empty string).

I assume the C++ standard inherits the same specification.
Jun 27 '08 #8
On Jun 21, 12:11 pm, Thomas Børlum <bor...@gmail.c omwrote:
Hey all,

I'm writing a c++ program that needs to read a file. I'm trying to read
a file that is in the same directory as the executable.

Everything works fine if I execute the program while in the program's
directory. What I need to do is read that file regardless of where
(cwd) I execute the program from, without hardcoding the absolute since
the program might be moved or be in differant locations on other
computers.

What should I do?

PS. I'm using the boost filesystem framework if it helps.
You may use getenv, doing something like this:
std::string source_dir = getenv( "srcdir" );

Look at http://www.cplusplus.com/reference/c...ib/getenv.html,
that there are another examples.
Jun 27 '08 #9
"Alf P. Steinbach" <al***@start.no kirjutas:
* James Kanze:
[...]
>Of course, this requirement is ignored more often than it is
met; Unix doesn't make the program name available in any shape,
form or fashion, and I don't think Windows does either,

Huh?
From Windows SDK documentation:

LPTSTR WINAPI GetCommandLine( void);

The return value is a pointer to the command-line string for the current
process.
....
Note The name of the executable in the command line that the operating
system provides to a process is not necessarily identical to that in the
command line that the calling process gives to the CreateProcess
function. The operating system may prepend a fully qualified path to an
executable name that is provided without a fully qualified path.

So it seems Windows makes it available in some shape or fashion ;-)

Regards
Paavo
Jun 27 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
8151
by: GunFro | last post by:
Hi. I want php.ini in my own place, say /etc not default /usr/local/bin. So when I configure I use --with-config-file-path=/etc .... Easy !? It dose not work, well it configure, make and make install ok but still looking for php.ini in /usr/local/bin. A script with phpinfo() tells me it looks in /etc but it still php.ini from /usr/local/bin. Using: Slackware 10.1, Apache 2.0.54, PHP 5.0.4
7
1871
by: middletree | last post by:
I usually use SQL Server, but have occasionally had to use Access. This is such an occasion. My research, mainly at aspfaq.com, has been that you need to put the file path in your connection string, like this (watch out for the wrap): strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\mydirectoryname\dbname.mdb;User Id=xxxxx;Password=xxxxxxx;" Well, the host of this new site I am working on is asking me if a relative
2
1723
by: Teis Draiby | last post by:
(Using C#) Question 1: Is there an easy build-in support for determining wether a given file path is 1) A valid file path (Only legal characters) 2) The file exists 3) The path exists
1
3308
by: Alex VanderWoude | last post by:
I am trying to <include> some text into an XML documentation topic, but that text is stored in a file that is in a different directory than the "current" XML file. Using a relative path does not appear to work, because the base being used is the Common7\IDE directory, not my original source directory. Here is the situation: MyApp directory contains the file Form1.cs, Form1.xml, and Generic.xml. There is also a subdirectory called...
2
6426
by: Harshdeep Mehta | last post by:
Hi all gurus, Myself Harshdeep Mehta, you can call me Harsh. I struck up in a situation where I need to define relative path in Web.Config. I.e. suppose I have a "Export" named folder, besides Web.Config in same location or somewhere in reference to web.config. Then how to define that. I know one easy solution but need better than that. the easy solution is
1
4029
by: Peted | last post by:
Hello bellow is select sample of code im using to dynamicaly load assembley's and it works, but the assembley.loadfile requires that i use a absolute directory path. Is there anyway i can use a relative file path. I just want to access the dll in subdirectory "deviceinterface" to where the application is located. (regardeless where the app is located)
1
1479
by: Bambers | last post by:
hi there, i have created a vb.net compact framework program for a mobile pc. i now need to upload a text file produced by the program onto my server. the file always remains in the same place on the moble pc, so i would like to have the extension for the file preset in the php coding so i dont have to brousefor the file. so basically i just want a php form with one button, by clicking it it will procees the file and upload it to the...
1
1451
by: BlouHond | last post by:
Hi, I'm using VB.net 2005. My dataset is called DsDetails1 My XML file is called details.xml When I use ReadXML, is there any way that I can set the path of this XML file to be read to be the root of my VB sollution. I need to be able to move the project around to different computers while coding, and I do not want to change the connection string every time. tried: DsDetails1.ReadXml("|DataDirectory|\Details.xml") as with a...
3
3231
by: Peter Wang | last post by:
Hi, all. I recently encountered a very annoying problem while using Zend Framework(ZF). We use ZF in our web application, and it works fine at the beginning, but later when concurrent requests goes high, we get very high cpu load. when i trace the httpd using strace, i find so much fstat64 syscalls, most of which failed, all these syscalls take more than 60% of cpu usage. After i check our php code carefully, i find
0
8462
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8802
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8586
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8658
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7405
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5682
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2792
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2028
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1787
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.