Detect Current Directory

This is a discussion on Detect Current Directory within the C Programming forums, part of the General Programming Boards category; I cannot for the life of me figure out how to detect the current directory my C application is executing ...

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    5

    Detect Current Directory

    I cannot for the life of me figure out how to detect the current directory my C application is executing from!

    It is simply driving me nuts.

    For now I've resorted to a really gross workaround.
    Calling System with the command "echo %cd% > dir.txt"
    then reading dir.txt to find out the directory


    There MUST be a more elegant way though, no?

    I've spent about 2 hours searching so far, it's been driving me up the wall!

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    By definition it would be operating system-specific. On the guess that you seem to be on Windows, you would need to ask Windows to tell you the current working directory.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    How about looking at argv[0]. Shouldn't that contain the full path name?

    I think it does in all modern operating systems. In ancient times it would only show you the filename.exe part.

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I need to double check the standard, but I am almost certain it is supposed to show the full path, nonoob. strrchr() to the rescue!

  5. #5
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Hmmm well I am still looking into it, but I do notice according to the C99 standard, if argc is 0 then argv can be either NULL or an empty string. So there is not an absolute guarantee for there to even be an argv[0]!

  6. #6
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    Has that ever happened?

  7. #7
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I am looking at the standard and...

    If the value of argc is greater than zero, the string pointed to by argv[0]
    represents the program name;
    So that is a no.

  8. #8
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    You can write your own C entry into a program in assembler. So I s'pose if you wrote a retarded C entry, then surely it can happen. But to answer your question, no. Though I have always unwittingly used controls such as "if(argc > 1)" and such for all of my command line parsers. So I can say with confidence that all of my programs could cope with an argv[0] = NULL.

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    According to my test that I just ran, gcc/MinGW on Windows just gives filename, not a full path. Visual Studio also just gives filename.

    (Or at least for both of these when run from the command line.)

  10. #10
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    There probably is a standard way to do this, I just don't know it off hand. I had never really even tried it like that before either, tabstop. Though since the standard says that is the information that should be contained within argv[0] it looks like those compilers conform.

  11. #11
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    That's odd, tabstop. At work I'm running Visual Studio 2005... Windows XP, and I get full pathname.

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I should be more specific. If I run using F5, I get the full path name. If I run from the command line, I get exactly what I typed at the command line (if I just type "temp", I get "temp"; if I type "temp.exe" I get "temp.exe", if I type ".\temp" I get ".\temp", and so on).

  13. #13
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    Oh OK. I too should be more specific. I ran it in "Debug" mode. Actually I click on the green arrow I think it is for "compile and run". I get a temporary cmd window with the result.

    I don't have it in front of me now.... I'm now at home on a Mac using Xcode. So don't mind me if I ever get things confused.

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Yeah, F5 is Debug mode too.

    I'm guessing, since Xcode is gcc under the hood, that it will be eerily similar to previous results.

  15. #15
    Registered User
    Join Date
    Sep 2008
    Posts
    5
    Thanks for looking into this more for me
    It seems I need to investigate the Win32 API

    I've been riffling through the MSDN but it's a pain to navigate
    Is that where I should find the Win32 API on C?
    Can anyone link me directly? My searches always turn up C++.. frustrating..

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Couple errors please help :-D
    By JJJIrish05 in forum C Programming
    Replies: 9
    Last Post: 03-06-2008, 02:54 AM
  2. Immediate programming help! Please!
    By xMEGANx in forum C++ Programming
    Replies: 6
    Last Post: 02-20-2008, 12:52 PM
  3. Problem with simple case statements
    By shoobsie in forum C Programming
    Replies: 2
    Last Post: 05-08-2006, 09:39 AM
  4. compiler build error
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-30-2003, 10:16 AM
  5. linked list recursive function spaghetti
    By ... in forum C++ Programming
    Replies: 4
    Last Post: 09-02-2003, 03:53 PM

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