Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.
Thanks for the reply, but are you saying that NSSearchPathForDirectoriesInDomains works in a C++ file? It works just fine in my objective c but I cannot get this to work in a C++ file.

This works for me:

Code:
std::string str;
str = [[NSSearchPathForDirectoriesInDomains(  NSDocumentDirectory
                                            , NSUserDomainMask
                                            , YES) objectAtIndex:0] UTF8String];
 
Thanks Phoney but could you be a little more specific. I tried something like this, basically created a Global.h file that got the path using the NSSearch function but as soon as I included it in my C++ file it threw all sorts of errors. Any additional input would be great.

Write your own utility functions with C linkage that provide the full paths to the app bundle and the documents folder. You'll then be able to access these from your C++ code. You implement those functions using Objective-C.
 
I don't really have code handy for this but it would go something like this:

Code:
// Utilities.h
const char * DocumentsFolder(void);   // returns full path to Documents folder

// Utilities.m
const char * DocumentsFolder(void)
{
	NSArray*	documentDir = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
	NSString*	path = nil;
	
	if (documentDir)
	{
		path = [documentDir objectAtIndex:0];
	}
	
	return [path fileSystemRepresentation];
}

Utilities.h has no Objective-C code in it and can be included in your .cpp files.

There are other ways to manipulate this kind of code but this is the core of it.
 
Awesome! Thanks, that makes a lot of sense. I'll give that a try and post my code when I get it working.
 
Thanks for the reply, but are you saying that NSSearchPathForDirectoriesInDomains works in a C++ file? It works just fine in my objective c but I cannot get this to work in a C++ file.

No, an ObjC++ file, which has the extension .mm.

I do cross-platform stuff in C/C++ and join it with Cocoa (on Mac OS) where it needs to meet the GUI. So I fairly regularly mix C (.c), C++ (.cpp), ObjC++ (.mm) and ObjC (.m) code in projects.
 
Have you reviewed Files and Networking

I realize this is an old post but really hoping someone on this thread can help me out.

I'm trying to save a file to the iPhone documents directory using C++. I know the standard C++ file functions but don't know how to get the directory. The NSString, NSSearch, etc. referenced in all the examples are not available as it is not objective c. Any help would be appreciated.

Thanks!

http://developer.apple.com/iphone/l...de/FilesandNetworking/FilesandNetworking.html
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.