在iOS的fopen问题

出于某种原因,即使我已经将path转换为文档path,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; } 

试图打开文件名时会打印出下面的完整path(我删除了应用程序实际目录名称的确切值)

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

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

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

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

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