我试图直接打开文件等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];
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top