iOS中的fopen问题

jar*_*ryd 8 c fopen objective-c ios

由于某种原因,No such file or directory即使我已将路径转换为文档路径,fopen 也会给出错误.

NSString *docsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *filePath = [docsPath stringByAppendingPathComponent:filename];
Run Code Online (Sandbox Code Playgroud)

我使用该UTF8String方法传递NSString,并且仅在读取模式下打开

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

尝试打开文件名时打印出以下完整路径(我删除了应用程序实际目录名称的确切值)

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

为什么fopen没有检测到文件?我已将其添加到目标,甚至尝试将文件检查器中的位置设置更改为Relative to GroupRelative to Project

Jod*_*ins 10

您必须使用以下方法将NSString转换为char*,否则它将无法正确映射到文件系统.

    char const *path_cstr = [[NSFileManager defaultManager] fileSystemRepresentationWithPath:pathString];
Run Code Online (Sandbox Code Playgroud)

现在你可以直接在fopen()中使用它.