Thread: Print the path of the executable

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    3

    Print the path of the executable

    I have the program foo in the directory C:\exe\

    From the directory, I do :
    C:\loin>C:\exe\foo.exe

    I need that toto.exe prints the path of the directory into which it is located (in this example : "c:\exe").

    The system function getcwd returns C:\loin but not C:\exe.

    Please, I need a quick answer.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Given
    int main ( int argc, char *argv[] )

    You could try looking in argv[0] which should be the name of the program being executed.

    Whether this contains no path / relative path / absolute path is at the mercy of your shell (or some other program calling 'exec' or 'spawn' functions)
    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.

  3. #3
    Registered User
    Join Date
    Dec 2003
    Posts
    3
    I 've already looked in argv[0] and it does't contain any path.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I 've already looked in argv[0] and it does't contain any path.
    Then you'll have to do something non-portable. For example:
    Code:
    #include <stdio.h>
    #include <windows.h>
    
    int main ( void )
    {
      char buffer[BUFSIZ];
      char *filepart;
    
      GetFullPathName ( "somefile.txt", BUFSIZ, buffer, &filepart );
      puts ( buffer );
      puts ( filepart );
    
      return 0;
    }
    My best code is written with the delete key.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > I 've already looked in argv[0] and it does't contain any path.
    Post your test program and state your OS/Compiler
    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.

  6. #6
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Here is another Windows function.

    Code:
    BOOL GetAppPath(LPTSTR szPath, UINT cchPath) {
    	LPTSTR szLastSlash;
    
    	if ( !GetModuleFileName(NULL, szPath, cchPath) ) return FALSE;
    
    	szLastSlash = _tcsrchr(szPath, TEXT('\\\'));
    
    	if (!szLastSlash) return FALSE;
    
    	*(szLastSlash + 1) = TEXT('\0');
    
    	return TRUE;
    }

  7. #7
    Registered User
    Join Date
    Dec 2002
    Posts
    103

    Look Again

    Originally posted by Termos
    I 've already looked in argv[0] and it does't contain any path.
    Hi,

    I have written this program and i tested it and it very much works...
    Code:
    #include <stdio.h>
    
    int main(int argc, char *argv[])
    {
      printf("The path of the command is : %s\n", argv[0]);
      return 0;
    }
    program exists in the directory
    /home/shiv/a.out

    running the program from:
    /etc/bin$ /home/shiv/a.out

    output is: /home/shiv/a.out
    Have a wonderful day.... and keep smiling... you look terrific that way
    signing off...
    shiv... as i know him

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I have written this program and i tested it and it very much works...
    The contents of argv[0] are implementation-defined. They could be the path, the file name, or even "\0". You cannot rely on youor results except on your exact implementation.
    My best code is written with the delete key.

  9. #9
    Registered User
    Join Date
    Dec 2003
    Posts
    3
    Originally posted by Salem
    > I 've already looked in argv[0] and it does't contain any path.
    Post your test program and state your OS/Compiler
    Quite simple :

    Code:
    #include <stdio.h>
    #include <stdlib.h> 
    
    #define TAILLE_CHAINE 1024
    
    int main(int argc, char** argv, char *envp[]) {
    
        char pathCourant[TAILLE_CHAINE];
    
        if (!getcwd (pathCourant, TAILLE_CHAINE)) return -1;
    
        printf("Directory : %s\n", pathCourant);
    
       return 0;
    
    }
    Running from C:\exe => C:\exe
    Running from C:\loin => C:\loin whereas I want C:\exe

    OS : Windows 2k but it must work either on Win and Linux
    Compiler : gcc (3.3.2 version I think (I can't get the version from the command line)) provided by Dev-C++

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    You're quite obviously stupid
    Just how many posts telling you about argv[0] are you going to ignore?
    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.

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >You're quite obviously stupid
    Classic. This is why we love you Salem.

    >#include <stdio.h>
    >#include <stdlib.h>
    >if (!getcwd (pathCourant, TAILLE_CHAINE)) return -1;
    Wow, and here I thought getcwd was declared in unistd.h for Dev-C++. I guess their include files lie.
    My best code is written with the delete key.

  12. #12
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >but could you use the chdir() function while you haven't reached / in linux
    Why? chdir changes the current working directory, it doesn't give you the path at all.

    >just don't call me stupid
    Okay, but your solution makes no sense concerning the original question.
    My best code is written with the delete key.

  13. #13
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    I don't want to be regaurded as an idiot, but could you use the chdir() function while you haven't reached / in linux. Would this work in windows? It should work like this in linux(I think,just don't call me stupid )
    Code:
    #include <stdio.h>
    #include <unistd.h>
    
    int main(void){
             int x;
             do{
                  x=chdir("..");
                 }
              while(x!=-1);
    }
    now how would you print the current directory? if so you could add a printf() statement. Also is there a pwd() function in windows. You could just system it if you do.
    [edit]Accidentaly deleted[/edit]

  14. #14
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    sorry got off track

  15. #15

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Print function: sending a function.. through a function?
    By scarlet00014 in forum C Programming
    Replies: 3
    Last Post: 11-05-2008, 05:03 PM
  2. merging linked lists
    By scwizzo in forum C++ Programming
    Replies: 15
    Last Post: 09-14-2008, 05:07 PM
  3. Replies: 1
    Last Post: 07-31-2002, 11:35 AM
  4. send data to printer and print on paper
    By ooosawaddee3 in forum C Programming
    Replies: 1
    Last Post: 07-22-2002, 05:19 AM
  5. How to print out the data throw printer.Attn Salem
    By Jason in forum C Programming
    Replies: 2
    Last Post: 09-23-2001, 05:58 AM