Thread: Get the Working Directory

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    45

    Get the Working Directory

    Hi,

    Now ive got the dynamic memory sorted. Is there any function / class in the c++ std that returns the path of the working directory.(the path of the directory which the application is in)

    Cheers
    Alex

  2. #2
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    One way is to use the first argument when defining main with command line arguments support. It gives you the full path of the executable.

    Read http://www.cprogramming.com/tutorial/lesson14.html
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> It gives you the full path of the executable.
    That is not necessarily true. Often only the executable name is available (as your link points out).

    Getting the working directory is a platform specific operation, so what OS and compiler are you using?

  4. #4
    Registered User
    Join Date
    Aug 2006
    Posts
    45
    Quote Originally Posted by Daved
    >> It gives you the full path of the executable.
    That is not necessarily true. Often only the executable name is available (as your link points out).

    Getting the working directory is a platform specific operation, so what OS and compiler are you using?
    well for the current project win32 visualc++

    Edit: I found a func: getcwd(), seems to work on linux, win32 & solaris

    -Alex
    Last edited by appleGuy; 08-28-2006 at 03:45 PM.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    getcwd() is a POSIX function, so it should work on quite a few systems.
    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.

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >I found a func: getcwd()
    Are you looking for the current working directory of the user, or the directory in which the executable resides?

  7. #7
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Quote Originally Posted by Daved
    >> It gives you the full path of the executable.
    That is not necessarily true. Often only the executable name is available (as your link points out).
    Yup. My bad. And I just checked and mingw doesn't give the full path. So, I have no idea why I was convinced that to be true.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  8. #8
    Registered User mrafcho001's Avatar
    Join Date
    Jan 2005
    Posts
    483
    http://msdn.microsoft.com/library/de...lefilename.asp
    http://msdn.microsoft.com/library/de...lebasename.asp

    one retrieves the path with the file name the other just the file name.. figure it out.

    EDIT:
    Code:
    TCHAR FileName[MAX_PATH] = {0};
    TCHAR FilePath[MAX_PATH] = {0};
    GetModuleFileNameEx(GetCurrentProcess(), NULL, (LPWSTR)&FilePath, MAX_PATH);
    GetModuleBaseName(GetCurrentProcess(), NULL, (LPWSTR)&FileName, MAX_PATH);
    size_t FP = wcslen((wchar_t*)&FilePath);
    size_t FN = wcslen((wchar_t*)&FileName);
    FilePath[FP-FN] = L'\0';
    there...
    Last edited by mrafcho001; 08-28-2006 at 08:01 PM.
    My Website
    010000110010101100101011
    Add Color To Your Code!

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    That's how you get the true name of the executable, but I think the OP was looking for getcwd():
    Is there any function / class in the c++ std that returns the path of the working directory.
    Of course, you could call GetModuleFileName() and strip off the file name itself to get the directory.
    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.

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Quote Originally Posted by appleGuy
    the working directory.(the path of the directory which the application is in)
    Note: these are NOT the same!
    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

  11. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You can change the working directory, or the program can start in a different directory than that which the executable is in. (Try it: right-click, create shortcut, right-click, properties and you can change the working directory.)

    Which one you want (working or executable directory) depends on what you're trying to do.
    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. Working directory pragma
    By mikahell in forum C++ Programming
    Replies: 6
    Last Post: 11-01-2007, 10:24 AM
  2. Directory help.
    By mrafcho001 in forum C++ Programming
    Replies: 4
    Last Post: 03-19-2005, 06:38 PM
  3. Directory crawler problems
    By mart_man00 in forum C Programming
    Replies: 31
    Last Post: 08-17-2003, 09:33 PM
  4. Replies: 6
    Last Post: 07-30-2003, 03:08 AM
  5. Working with directories...
    By C Seņor in forum C Programming
    Replies: 4
    Last Post: 04-20-2002, 11:45 AM