341008 341008 - 2 months ago 5x
C Question

Writing to file in iOS

Is it possible to create a text file using C/C++ calls in iOS? Is it then possible to open and read the file from outside the application? That is, application A creates a text file somewhere it has permissions to write to and application B then reads from it. Or, forget application B, can I just open it and read?



Use NSFileHandle to get read/write access to files and NSFileManager to create directories and list their contents and do all other sorts of file system access.

Just keep in mind that every App is sandboxed and can only write to its own designated directories. You can get the path to these directories with code like this:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;

However, you can not access files of another App.

iTunes automatically backs up these directories, so they are automatically restored if the user does a device restore. Also keep in mind that you should not "expose" the file system to the App users in form of file-browsers or "save dialogs", etc.