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