![](/img/trans.png)
[英]how to open file.xlsx in UIWebView IOS using objective-c
[英]How to open a file in iOS using C?
解决方案
没有错误,我没注意。 感谢 Rob 指出这一点
我有一个需要使用 C 库读取文件的 iOS 应用程序。
当我尝试从 C function 访问文件时,我只收到
权限被拒绝错误 (13)
no such file or directory,我找不到原因。 当我使用 Swift 创建、读取或写入时,它不会发生。
我尝试了几种方法在用户数据容器内创建文件 URL,C 不起作用,它只适用于 Swift。
即使使用 Swift 创建文件,在 C 调用之前和之后打开、读取和验证文件,它也只会在 C 中出现错误。
// I tried different paths before and added the filename to these directories
// FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
// FileManager.default.urls(for: .libraryDirectory, in: .userDomainMask).first!
let path: String = NSTemporaryDirectory().appending("test_file.txt")
FileManager.default.fileExists(atPath: path.path)
// true
XCTAssertNoThrow(try "test".write(to: URL(fileURLWithPath: path), atomically: true, encoding: .utf8))
XCTAssertNoThrow(try FileManager.default.setAttributes([FileAttributeKey.posixPermissions: 0o777], ofItemAtPath: path))
// call C
test_read_file(path)
FileManager.default.fileExists(atPath: path.path)
// true
// test_read_file.c
int test_read_file(const char* path) {
access(file_path, W_OK);
// returns 0
open(path, O_RDONLY);
// returns 13
// errno is 2 (ENOENT: No such file or directory)
return 0;
}
更正
errno
值为2
ENOENT 2 No such file or directory
13 返回值是文件句柄,而不是错误代码,表明您的代码完全按照您的预期工作。
open
返回一个非负值表示成功,-1 表示失败。 要检查特定错误,请检查errno
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.