ios - 在 iOS 上打开时得到 -1,为什么?

标签 ios iphone file-io posix file-descriptor

代码复制如下。我正在使用文档目录,因为我知道您可以在应用程序沙箱之外编写。 我还比较了我用来确定 NSFileManager 创建的路径的字符串,它们是相同的。大家觉得怎么样?

- (NSString *)documentsDirectory
{
    NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);

    return [paths objectAtIndex:0];
}

- (void) whateverFunction{
    NSString *memfileName = @"memmapfile.map";

    NSString *filePath = [[self documentsDirectory] stringByAppendingPathComponent:memfileName];

    NSLog(@"Here is the filePath: %@", filePath);

    NSLog(@"Other        version: %s", [[NSFileManager defaultManager] fileSystemRepresentationWithPath:filePath]);
    int memFD = open([filePath cStringUsingEncoding:NSASCIIStringEncoding], O_RDWR);
    NSLog(@"I am getting -1 for memFD: %d", memFD);
}

最佳答案

我猜你的文件还不存在,所以你当然无法打开它。您应该将 O_CREAT 添加到打开标志中,以便在不存在时创建它:

int memFD = open([filePath cStringUsingEncoding:NSASCIIStringEncoding], O_RDWR|O_CREAT);

关于ios - 在 iOS 上打开时得到 -1,为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14736614/

相关文章:

c++ - Win32 文件锁定读取 : how to find out who's locking them

ios - 在 UiNavigationBar 上禁用多点触摸

iphone - uiscrollview 和 uiimageview subview 中没有垂直滚动

javascript - Sencha Touch 2 + PhoneGap + iPad : Video with base64 encoded data: "The Operation could not be completed"

ios - NSDate dateWithTimeIntervalSince1970 添加额外的一天

iphone - 由于 iphone 自动锁定导致应用程序崩溃?

python - 在保持串行读取的同时压缩一系列 JSON 对象?

python - 如何在 Python 中删除文件或文件夹?

ios - 如何处理 Json 中的 <null> 在 objective-c 中崩溃?

objective-c - iOS 面朝上方向问题