Getting the path file of your program?

This is a discussion on Getting the path file of your program? within the C++ Programming forums, part of the General Programming Boards category; If I wanted to get the full path of my running program is there a C++ equivalent to App.Path or ...

  1. #1
    Registered User
    Join Date
    Jun 2005
    Location
    Philadelphia
    Posts
    16

    Question Getting the path file of your program?

    If I wanted to get the full path of my running program is there a C++ equivalent to App.Path or does this have to be done in a long and tedious process?

  2. #2
    Registered User mikahell's Avatar
    Join Date
    Jun 2006
    Posts
    114
    Luckily, the answer to this is quite, too easy! It should work fine with what you are looking for, it's a special "#define" that will tell you the whole path to your application... Then you just have to place that piece of code where you need it:

    Code:
    __FILE__
    There are probably plenty of those special #defines, though the other one I know are these, and what they do is enough relevent:

    Code:
    __LINE__
    __DATE__
    __TIME__

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by mikahell
    Luckily, the answer to this is quite, too easy! It should work fine with what you are looking for, it's a special "#define" that will tell you the whole path to your application... Then you just have to place that piece of code where you need it:

    Code:
    __FILE__
    That would be the source file, not the name of the executable.

    RedZone: What OS?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    903
    You are definitely wrong. The __FILE__ macro returns the path of the current file (.h, .hpp, .c, .cpp, .cxx ....) and not the path of the current process:
    Code:
    HANDLE ThisProcess = GetCurrentProcess();
    TCHAR FilePath[MAX_PATH + 1] = {0};
    GetFinalPathNameByHandle(ThisProcess, FilePath, MAX_PATH, 0);

  5. #5
    Registered User
    Join Date
    Jun 2005
    Location
    Philadelphia
    Posts
    16
    Quote Originally Posted by Dave_Sinkula
    That would be the source file, not the name of the executable.

    RedZone: What OS?
    Windows XP

    Quote Originally Posted by Desolation
    HANDLE ThisProcess = GetCurrentProcess();
    TCHAR FilePath[MAX_PATH + 1] = {0};
    GetFinalPathNameByHandle(ThisProcess, FilePath, MAX_PATH, 0);
    This is helpful but MSDN tells me that I need Vista.

  6. #6
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    You have to search the MSDN a bit, I have found some of their stuff is for Win CE, then when i search and click again on a function, I get Win XP

  7. #7
    Registered User
    Join Date
    May 2005
    Location
    Texas
    Posts
    103
    Really, It's not that hard....It comes all in one function:

    Code:
    DWORD GetModuleFileName(
      HMODULE hModule,
      LPTSTR lpFilename,
      DWORD nSize
    );
    In code like this:
    Code:
    char myFilename[MAX_PATH];
    GetModuleFileName(NULL,myFilename,MAX_PATH);
    
    //myFilename now has the directory
    Be easy on me...only 14

  8. #8
    Registered User
    Join Date
    Jun 2006
    Posts
    29
    im not sure but wont
    Code:
    #include<iostream>
    int main(int argc,char *argv[]){
    if(argc !=2){
    cout<<argv[0]<<endl;
    }
    }
    work?

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >im not sure but wont <snip code> work?
    No, argv[0] isn't required to be meaningful at all (ie. it can be an empty string), and even if it is meaningful, it isn't required to contain the full path.
    My best code is written with the delete key.

  10. #10
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,231
    It is a Win32 thread I think.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  11. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    On Windows it always has the full path as far as I can tell. But not with other OSes.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  12. #12
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Only because explorer and visual studio put in the full path. Try calling the program from the console and you'll see that you're wrong.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  13. #13
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    Or if you call it using an exec() function, you can completely lie about it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  14. #14
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Only because explorer and visual studio put in the full path. Try calling the program from the console and you'll see that you're wrong.
    On Windows 98, the full path is put in no matter how I execute the program; console or explorer or otherwise. (Except for exec(). ) I guess it isn't the same for other versions of windows?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 04:02 AM
  3. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  4. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21