iPhone - FOPEN و PathForresource - تم رفض إذن
-
19-09-2019 - |
سؤال
لقد حاولت فتح ملف مباشرة 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];
}
لا تنتمي إلى StackOverflow