Sign in to follow this  

Mac getcwd() not returning what I expect

This topic is 2639 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey all,

I'm working on a log system and am trying to figure out how to get the directory my program is running in so I can create a txt file to output the log messages to. For the longest time I've worked in windows where calling fopen or ofstream's constructor with just the filename would create the file in the spot I'm asking about.

When I look at the value returned by getcwd(), instead of seeing /user/me/desktop/logTest I get /private/tmp.

I'm so new to unix that I have no idea why my application would be running from here, and how to get what I'm actually looking for.

I'd prefer not to include boost in such a small subset of code, anyone out there got any tips/info that could help?

Many thanks!
-Croucho

Share this post


Link to post
Share on other sites
1) My guess is that your IDE set the CWD into /private/tmp, you can likely change that in the options somewhere.

BUT
You have no guarantee that the application directory is writeable. On unix your probably in /usr/bin and on windows c:/program files/. Neither of those directories have user write permissions unless you are root/admin.

1) You shouldn't be using getcwd() for your logs anyway. You should be using
something like getenv(“HOME”); on a unix system or SHGetFolderPath on a windows platform.

2) You should also have /tmp as a valid place to write to. And on unix the /var/log directory is where most apps write their actual log files.

3) You could use mkstemp as well.

Share this post


Link to post
Share on other sites
Quote:
Original post by croucho
When I look at the value returned by getcwd(), instead of seeing /user/me/desktop/logTest I get /private/tmp.
Since applications are typically installed in /Applications/, which is not writeable, any application launched from the Finder will have its working directory set to somewhere innocuous. If use to be the root directory, but Snow Leopard seems to have shifted this to the tmp directory to make things a little more sensible.
Quote:
I'm so new to unix that I have no idea why my application would be running from here, and how to get what I'm actually looking for.
If one really needs the path to the application, one has to bring the native APIs into the mix. For Cocoa/Obj-C this would be:
[[NSBundle mainBundle] bundlePath]
And for CoreFoundation/C/C++ this would be:
CFBundleCopyBundleURL(CFBundleGetMainBundle())
Conversion to a suitable c-string/std::string is left as an exercise (google can explain).
Quote:
I'd prefer not to include boost in such a small subset of code, anyone out there got any tips/info that could help?
The correct location to store log files under OS X is ~/Library/Logs/<app-name>.log

Share this post


Link to post
Share on other sites
Sign in to follow this