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??
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.
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.
Re: There is a function that returns my program`s pos(dir) in STL??
Quote:
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!)
Re: There is a function that returns my program`s pos(dir) in STL??
Quote:
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.
Re: There is a function that returns my program`s pos(dir) in STL??
Quote:
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).
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)!!!
Re: There is a function that returns my program`s pos(dir) in STL??
Quote:
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
Re: There is a function that returns my program`s pos(dir) in STL??
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.
Re: There is a function that returns my program`s pos(dir) in STL??
Quote:
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.