CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    May 2011
    Posts
    5

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

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    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

  3. #3
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    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.

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: There is a function that returns my program`s pos(dir) in STL??

    Quote Originally Posted by Eri523 View Post
    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

  5. #5
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    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.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  6. #6
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: There is a function that returns my program`s pos(dir) in STL??

    Quote Originally Posted by laserlight View Post
    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.

  7. #7
    Join Date
    May 2011
    Posts
    5

    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)!!!

  8. #8
    Join Date
    Apr 1999
    Posts
    27,449

    Re: There is a function that returns my program`s pos(dir) in STL??

    Quote Originally Posted by kangdh2140 View Post
    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

  9. #9
    Join Date
    May 2011
    Posts
    5

    Re: There is a function that returns my program`s pos(dir) in STL??

    thank
    u

  10. #10
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    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.

  11. #11
    Join Date
    May 2011
    Posts
    5

    Re: There is a function that returns my program`s pos(dir) in STL??

    Quote Originally Posted by Lindley View Post
    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
  •  





Click Here to Expand Forum to Full Width

Featured