首页 文章

iOS中的fopen问题

提问于
浏览
6

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

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

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

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

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

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

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

1 回答

  • 9

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

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

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

相关问题