Last active
December 11, 2015 14:48
-
-
Save mopsled/4616532 to your computer and use it in GitHub Desktop.
Drop-in fopen replacement for iOS applications that prepends the documents directory to file paths.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// utilities.h | |
// | |
#include <stdio.h> | |
#ifndef MyProject_utilities_h | |
#define MyProject_utilities_h | |
FILE *iosfopen(const char *filename, const char *mode); | |
#endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// utilities.m | |
// | |
#include "utilities.h" | |
#include <Foundation/Foundation.h> | |
FILE *iosfopen(const char *filename, const char *mode) { | |
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); | |
NSString *documentsDirectory = [paths objectAtIndex:0]; | |
NSString *fileString = [NSString stringWithCString:filename encoding:NSASCIIStringEncoding]; | |
NSString *path = [documentsDirectory stringByAppendingPathComponent:fileString]; | |
const char *filePath = [path cStringUsingEncoding:NSASCIIStringEncoding]; | |
return fopen(filePath, mode); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment