Thread: How to get the current directory and the file name

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    10

    How to get the current directory and the file name

    Hi, I'm looking for a code that gets the directory where the file is being executed (for example: C:\some\directory) and the name of the file that is being executed.(for example: someprogram.exe)

    Thanks in advance.

  2. #2
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    In windows there is GetCurrentDirectory
    In linux maybe this

    For the program name you can do int main(int argc, char** argv) and read argv[0]. Which is a char array that contains the name of the executable. But NOT always. But you can try it. I believe it is true at least for windows and some linux OS.

  3. #3

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by C_ntua View Post
    In windows there is GetCurrentDirectory
    In linux maybe this

    For the program name you can do int main(int argc, char** argv) and read argv[0]. Which is a char array that contains the name of the executable. But NOT always. But you can try it. I believe it is true at least for windows and some linux OS.
    Windows has getcwd() also, but Microsoft likes to annoy people by putting a '_' infront of POSIX function names.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by C_ntua View Post
    In windows there is GetCurrentDirectory
    In linux maybe this

    For the program name you can do int main(int argc, char** argv) and read argv[0]. Which is a char array that contains the name of the executable. But NOT always. But you can try it. I believe it is true at least for windows and some linux OS.
    another option on linux (this example assumes C++, but it can easily be done in C as well):

    Code:
    #include <string>
    #include <cstdlib>
    
    int main(void)
    {
      std::string mycwd(getenv("PWD"));
      std::cout << "Current Directory: " << mycwd << std::endl;
      return 0;
    }

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elkvis View Post
    another option on linux (this example assumes C++, but it can easily be done in C as well):

    Code:
    #include <string>
    #include <cstdlib>
    
    int main(void)
    {
      std::string mycwd(getenv("PWD"));
      std::cout << "Current Directory: " << mycwd << std::endl;
      return 0;
    }
    That assumes that the environment variable PWD is actually in sync with the current directory.

    Note also that an application can be started in a different directory than the application actually lives in, so if you want to know what directory the application actually lives in, it's can be difficult. In a well-behaved system, argv[0] contains the application name and location, but there's nothing preventing someone from writing code like this (in Linux/Unix):
    Code:
       execl("/somepath/someapp", "rubbish", "arg1", NULL);
    argv[0] will then be "rubbish", but the application is "someapp" and loaded from "/somepath".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Jan 2009
    Posts
    10
    Thanks for all of your answers. One thing I forgot to mention is that I'm using C, not C++.
    The code in fact was very simple, and is made, as you said, using GetModuleFileName. Here is the code:

    Code:
    #include <stdio.h>
    
    #include <stdlib.h>
    
    #include <windows.h>
    
    
    
    int main()
    
    {
    
        char path[100];
    
    
    
        GetModuleFileName(NULL, path, sizeof(path));
    
        printf("%s\n", path);
    
        system("pause");
    
        return 0;
    
    }
    Thanks for all of your help =D

Popular pages Recent additions subscribe to a feed