🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Mac getcwd() not returning what I expect

Started by
3 comments, last by swiftcoder 13 years, 9 months ago
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
Advertisement
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.

Many thanks for the help KulSeran, you advice helped me get just what I was looking for :)
Many thanks for the help KulSeran, you advice helped me get just what I was looking for :)
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

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

This topic is closed to new replies.

Advertisement