IOS fopen issue

For some reason, fopen gives an error No such file or directory, although I converted the path to the document path.

NSString *docsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *filePath = [docsPath stringByAppendingPathComponent:filename];

I pass NSString using a method UTF8Stringand open in read only mode with

if ((fh=fopen(filename, "r+b")) == NULL){
    perror("fopen");
    return -1;
}

When I try to open the file name, the following full path is printed (I deleted the exact values ​​for the name of the actual application name)

/VAR/mobile/Applications/#####//Documents/testImages.pak

Why don't fopen detect the file? I added it to the target and even tried changing the location settings in the file inspector to Relative to GroupandRelative to Project

+5
source share
1 answer

You need to convert NSString to char * using the following method, or it will not display correctly on the file system.

    char const *path_cstr = [[NSFileManager defaultManager] fileSystemRepresentationWithPath:pathString];

fopen().

+7

All Articles