Get the Working Directory

This is a discussion on Get the Working Directory within the C++ Programming forums, part of the General Programming Boards category; Hi, Now ive got the dynamic memory sorted. Is there any function / class in the c++ std that returns ...

  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
    Portugal
    Posts
    7,581
    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
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    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,344
    >> 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 04:45 PM.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    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
    Portugal
    Posts
    7,581
    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.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    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 09: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,048
    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,893
    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,048
    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, 11: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, 10:33 PM
  4. Replies: 6
    Last Post: 07-30-2003, 04:08 AM
  5. Working with directories...
    By C Señor in forum C Programming
    Replies: 4
    Last Post: 04-20-2002, 12:45 PM

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