How to get Current working Directory?

Discussion in 'Mac Programming' started by trey5498, Jul 17, 2008.

  1. trey5498 macrumors regular

    Joined:
    Jun 16, 2008
    #1
    How do I get the current working directory of the application? in the command or shell line it is "./test.txt" However setting that to a path doesnt seem to work.

    I have seen:

    GetCurrentDirectory, but I can't seem to track that one down either. Any ideas?
     
  2. toddburch macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
  3. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #3
    On most unix systems you can get the environment variable PWD with getenv. This may also work, but I am not familiar with how it behaves:
    Code:
    NSString *myPath = [[NSBundle mainBundle] bundlePath];
    Lee
     
  4. yeroen macrumors 6502a

    yeroen

    Joined:
    Mar 8, 2007
    Location:
    Cambridge, MA
    #4
    You can use getcwd from the standard C library. Do a man on it for usage.
     
  5. trey5498 thread starter macrumors regular

    Joined:
    Jun 16, 2008
    #5
    How to obtain just the path and not the file name?
     
  6. yeroen macrumors 6502a

    yeroen

    Joined:
    Mar 8, 2007
    Location:
    Cambridge, MA
    #6
    getcwd()
     
  7. trey5498 thread starter macrumors regular

    Joined:
    Jun 16, 2008
    #7
    Code:
    NSString *myPath = [[NSBundle mainBundle] bundlePath];
    
    got the path in one line where getcwd wanted to do it in like 3 and I kept getting errors. I am trying to do it in as much cocoa as I can.
     
  8. HiRez macrumors 603

    HiRez

    Joined:
    Jan 6, 2004
    Location:
    Western US
    #8
    Can't you use

    Code:
    NSString *curDir = [[NSFileManager defaultManager] currentDirectoryPath];
    ? Or is that not the same thing?
     
  9. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #9
    This is probably better, I hadn't tried the call I suggested, just read it elsewhere.

    -Lee
     
  10. saurabhshukla macrumors newbie

    saurabhshukla

    Joined:
    Mar 20, 2009
    Location:
    India
    #10
    Not working in my code?????

    Hi Guys i tried both
    Code:
    NSString *myPath = [[NSBundle mainBundle] bundlePath];
    &
    NSString *curDir = [[NSFileManager defaultManager] currentDirectoryPath];
    Earlier due to some mistakes these were not getting work but now working. Thanks to all.
     

Share This Page