0

I'm making a program in C++ that needs to acces files in the folder it is in. The problem is that both getcwd() (from sys/stat.h) and the directory . (the current directory), refer to /usr/bin (or something else), but I want that to be the directory the executable is in. Does anyone know how to change this

I'm using XCode 5.0.1 on OS X Mavericks.

I already read this question, but I couldn't find the "get info" item in the context menu of my product

Community
  • 1
  • 1
Dirk
  • 2,094
  • 3
  • 25
  • 28

1 Answers1

1

If you're trying to do this programmatically, there are a number of ways. You can get the path to your bundle by calling bundlePath. It would look something like this:

NSBundle* mainBundle = [NSBundle mainBundle];
NSString* bundlePath = [mainBundle bundlePath];

If you want to do it with BSD calls, see this answer.

Once you have the path, you can use that with whichever file reading functions you want (fopen(), NSFileManager, etc.).

Community
  • 1
  • 1
user1118321
  • 25,567
  • 4
  • 55
  • 86
  • No, I can't use the foundation framework, I'm using C++, not Objective-C. Isn't there a way to do this in the project configuration oid? – Dirk Dec 01 '13 at 18:19
  • @user1833511 Did you read the answer I linked to? It answers the question without using Foundation. You probably don't want to set some setting in Xcode because then it won't be the same when you move the app to a different system or even a different directory. – user1118321 Dec 01 '13 at 18:25