|
-
May 19th, 2011, 02:28 AM
#1
There is a function that returns my program`s pos(dir) in STL??
There is a function that returns my program`s pos(e.g. C:\Documents and Settings\my example programs\) in STL??
If that is not, how can I code that function??
-
May 19th, 2011, 03:09 AM
#2
Re: There is a function that returns my program`s pos(dir) in STL??
I don't know about STL, however:
In Windows you can use GetModuleFileName API to get the full path name of the executable.
In UNIX you can use the argv[0] instead:
Code:
int main(int argc, char **argv)
{
...
}
Then just remove the file name to obtain its directory.
Victor Nijegorodov
-
May 19th, 2011, 01:59 PM
#3
Re: There is a function that returns my program`s pos(dir) in STL??
argv[0] works under Windows as well. AFAIK it's part of the C(++) standard. And I think it's definitely the simplest option here.
Try this:
Code:
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
cout << "I am " << argv[0] << endl;
return 0;
}
Note that argv[0] contains the whole executable path including the file name, not just the directory as in your example in the OP.
I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.
This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.
-
May 19th, 2011, 02:15 PM
#4
Re: There is a function that returns my program`s pos(dir) in STL??
 Originally Posted by Eri523
argv[0] works under Windows as well. AFAIK it's part of the C(++) standard. And I think it's definitely the simplest option here.
But not in Win32-based application where WinMain is used instead on main. Well, it is still possible to use WinMain parameter lpCmdLine and parse it to obtain the first one or use GetCommandLine API and parse it...
but GetModuleFileName is simpler to use (no parsing is needed!)
Victor Nijegorodov
-
May 19th, 2011, 02:19 PM
#5
Re: There is a function that returns my program`s pos(dir) in STL??
 Originally Posted by Eri523
AFAIK it's part of the C(++) standard.
It is, but whether it contains the full path or not is implementation defined. It could even be an empty string.
-
May 19th, 2011, 02:36 PM
#6
Re: There is a function that returns my program`s pos(dir) in STL??
 Originally Posted by laserlight
It is, but whether it contains the full path or not is implementation defined. It could even be an empty string.
Of course you're right. I remember using an ancient MS C compiler under DOS and its runtime returned "C" in argv[0] when the program was run under a DOS version below 3.0. However, that simply was because these even-more-ancient DOS versions didn't provide that information to the runtime.
I think nowadays it should work in practically all implementations, provided the program starting mechanism is file-system-based at all (which may, for instance, not be the case in some embedded applications).
I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.
This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.
-
May 19th, 2011, 05:06 PM
#7
Re: There is a function that returns my program`s pos(dir) in STL??
I want only dir(e.g. C:\Windows\system32\)!!!
Not program`s path(e.g. C:\Windows\system32\cmd.exe)!!!
-
May 19th, 2011, 05:22 PM
#8
Re: There is a function that returns my program`s pos(dir) in STL??
 Originally Posted by kangdh2140
I want only dir(e.g. C:\Windows\system32\)!!!
Not program`s path(e.g. C:\Windows\system32\cmd.exe)!!!
Why all the exclamation points???!!!!!!!!!
If you get the entire path, then just do this to get the path from a full name:
http://msdn.microsoft.com/en-us/libr...=vs.80%29.aspx
Regards,
Paul McKenzie
-
May 19th, 2011, 08:25 PM
#9
Re: There is a function that returns my program`s pos(dir) in STL??
-
May 19th, 2011, 09:50 PM
#10
Re: There is a function that returns my program`s pos(dir) in STL??
It's relatively easy to write your own method to find the last \ or / in a given string and take the part of the string before it.
-
May 19th, 2011, 10:23 PM
#11
Re: There is a function that returns my program`s pos(dir) in STL??
 Originally Posted by Lindley
It's relatively easy to write your own method to find the last \ or / in a given string and take the part of the string before it.
thank u.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|