iPhone - fopen 和 pathForResource - 权限被拒绝

标签 iphone filesystems fopen

我尝试直接打开文件,例如 fopen("/test.txt","w+");它可以在模拟器上运行,但不能在 iPhone 上运行。

然后我尝试了这个:

NSString *path = [[NSBundle mainBundle] pathForResource: @"GirlName.txt" ofType:nil];
NSLog(path);
fichier = fopen([path cStringUsingEncoding:1],"w+");

if (fichier != NULL)
{
    ...
}
else
{
    perror("error ");
}

我在控制台中收到权限被拒绝:

2009-07-24 17:17:27.415 Mademoiselle[897:20b]
/var/mobile/Applications/.../....app/GirlName.txt
error : Permission denied

你能告诉我出了什么问题吗?

最佳答案

您无法打开应用程序包中的文件并使其可写(“w+”)。

如果您将模式更改为“r”,相同的代码可能会起作用(我尚未测试过)

如果你需要一个可写的文本文件,你需要放在一个可写的目录中,我会这样做:

//Method writes a string to a text file
-(void) writeToTextFile{
    //get the documents directory:
    NSArray *paths = NSSearchPathForDirectoriesInDomains
        (NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    //make a file name to write the data to using the documents directory:
    NSString *fileName = [NSString stringWithFormat:@"%@/textfile.txt", 
                         documentsDirectory];
    NSString *content = @"Test Copy Here";
    //save content to the documents directory
    [content writeToFile:fileName 
              atomically:NO
                encoding:NSStringEncodingConversionAllowLossy 
                   error:nil];
}

关于iPhone - fopen 和 pathForResource - 权限被拒绝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1178432/

相关文章:

iphone - Core Location 无法在 ios 模拟器上运行

iphone - 无法将应用程序包转换为 NSData

image - 存储数百万张图像

windows - 如何制作从 docker linux 容器内部制作的符号链接(symbolic link),以便从 Windows 主机上看到(如果需要,可能涉及 samba)

c - 读取 2kb txt 文件时出现段错误 (c)

在读取模式下使用 fopen 检查文件是否存在不会给出 .或/文件名

ios - 我正在使用 alamofire 5 beta 版本进行 api 调用。在传递正确的参数后,我得到无效的响应参数

ios - 如果用户将 "Set Automatically"设置为关闭,如何检查正确的当前时间

linux - 如何让linux文件系统断电安全?

c - C中的fopen函数+写入文件