gpt4 book ai didi

objective-c - iOS 中的 fopen 问题

转载 作者:可可西里 更新时间:2023-11-01 03:08:38 24 4
gpt4 key购买 nike

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

最佳答案

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

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

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

关于objective-c - iOS 中的 fopen 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10664955/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com