iPhone - fopen 和 pathForResource - 权限被拒绝

发布于 2024-07-30 00:18:17 字数 543 浏览 1 评论 0原文

我尝试直接打开文件,例如 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

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

I've tried to open a file directly like fopen("/test.txt","w+"); it works on the simulator but doesn't work on the iPhone.

Then I've tried this:

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

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

I receive in console Permission denied:

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

Can you tell me what's wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

晒暮凉 2024-08-06 00:18:20

您无法打开应用程序包中的文件并使其可写(“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];
}

You cannot open a file within the application bundle and make it writable ("w+").

This same code will likely work (I have not tested it) if you change the mode to "r"

If you need a writable text file, you need to place in in a writable directory, I'd do something like this:

//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];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文