|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| ||||||||||||||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Path to program's directory
hi,
can anyone tell me how can i get full path to directory, where is my program placed? Thanks |
|
#2
|
||||
|
||||
|
For which OS/Compiler?
You could look at argv[0], but it's contents depend on your shell, and can be easily spoofed if you manipulate say execl() calls. |
|
#3
|
||||
|
||||
|
On DOS/Windows there are functions
Code:
getcwd, getdisk, getcurdir Code:
#include <stdio.h>
#include <dir.h>
int main(void)
{
char buffer[MAXPATH];
getcwd(buffer, MAXPATH);
printf("The current directory is: %s\n", buffer);
return 0;
}
On Linux platform try Code:
man 3 getcwd |
|
#4
|
|||
|
|||
|
Is this for any Visual C / C++ Applications by any odd chance?
__________________
-Jason Clark |
|
#6
|
||||
|
||||
|
Try looking over this page: http://autopackage.org/docs/binreloc/
See if that helps you any.
__________________
Spidermonkey Tutorial; Fight me: http://aoeex.mybrute.com http://www.aoeex.com/gmap.php - Put yourself on the map (Now Updated!) |
|
#7
|
||||
|
||||
|
Mareq, your answer only tells the current directory, not where the program is found. Most programs are found in one of the directories listed in the PATH variable.
Even then, you can add extra complexity to the problem by specifying a relative path to a program which isn't on the PATH, eg ..\foo\bar\prog.exe |
|
#8
|
||||
|
||||
|
Quote:
Current path is at start the path of progam, but yes, you're right, if you explicitly change current directory... (Is this true? I'm not sure.) |
|
#9
|
||||
|
||||
|
Quote:
That's not true. The current path at the start of execution is whatever path you were in. For instance say you open up a command prompt in XP and go to some directory (say your home dir, c:\documents and settings\yourname) to do something, then you execute notepad. notepad.exe is in C:\Windows, but the current path as returned by getcwd in notepad would be c:\documents and settings\yourname. |
|
#10
|
|||||
|
|||||
|
I use such code to get directory of my exe file:
C++ Code:
along the way, there is the full path. |
|
#11
|
|||
|
|||
|
thanks, it works on Windows, but I need it also on Linux ...
|
|
#12
|
||||
|
||||
|
Look into the binreloc package I linked to above. It should do what you want.
|
|
#13
|
|||
|
|||
|
I found the way how to get the path on Linux - it's in /proc/self/maps. So i wrote this function, which gets the path from this file:
Code:
char *wd;
wd=new char[100]; //alocate the memory
FILE *f;
char tmp[200];
char *tmp2,*tmp3;
f=fopen("/proc/self/maps","r"); //that file contains path to my aplication
fgets(tmp,200,f); //read the first line
fclose (f);
tmp[strlen(tmp)-1]='\0'; //remove newline character
tmp2=strchr(tmp,'/'); //find first slash
tmp3=strrchr(tmp,'/'); //find the last slash
*tmp3='\0'; //tarminating character replaces last slash of path
sprintf(wd,"%s",tmp2); //copy path on the other place in memory
return (wd);
Anyway, thasks for your help ![]() |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > C Programming > Path to program's directory |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|