builtin function for getting current directory

This is a discussion on builtin function for getting current directory within the C Programming forums, part of the General Programming Boards category; In my program I use chdir a number of times, and was wondering if there is a function that returns ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    50

    builtin function for getting current directory

    In my program I use chdir a number of times, and was wondering if there is a function that returns the directory in which the program is contained? I am looking for an alternative to:

    1) create a global variable original_root to store the path in at the beginning of program
    2) call getcwd(".") and store the returned value in variable original_root
    3) chdir(some_other_path1)
    4) chdir(some_other_path2)
    .
    .
    .
    x) chdir(some_other_pathn)
    x+1) chdir(original_root)


    Essentially, I'm wondering if there is some function that can be called at any time to give the path to the directory from which the program is being run, thus avoiding the need to save this directory in some variable. Thanks in advance!
    Last edited by nicoeschpiko; 05-07-2010 at 10:04 AM.

  2. #2
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    If you don't mind C++ and bringing in Boost as a dependency: Filesystem Reference

  3. #3
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,058
    Why not roll your own and call it whenever you need to return to the origin.

  4. #4
    Registered User
    Join Date
    Feb 2010
    Posts
    50
    Thanks for the ideas folks. Much appreciated!

  5. #5
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    argv[0] ?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bisection Method function value at root incorrect
    By mr_glass in forum C Programming
    Replies: 3
    Last Post: 11-10-2005, 09:10 AM
  2. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 11:03 AM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. linked list recursive function spaghetti
    By ... in forum C++ Programming
    Replies: 4
    Last Post: 09-02-2003, 03:53 PM
  5. Replies: 5
    Last Post: 02-08-2003, 07:42 PM

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