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