iPhone - fopen et pathForResource - Autorisation refusée
-
19-09-2019 - |
Question
J'ai essayé d'ouvrir un fichier directement comme fopen("/test.txt","w+")
; cela fonctionne sur le simulateur, mais ne fonctionne pas sur l'iPhone.
Je l'ai essayé ceci:
NSString *path = [[NSBundle mainBundle] pathForResource: @"GirlName.txt" ofType:nil];
NSLog(path);
fichier = fopen([path cStringUsingEncoding:1],"w+");
if (fichier != NULL)
{
...
}
else
{
perror("error ");
}
Je reçois dans l'autorisation de la console Refusé:
2009-07-24 17:17:27.415 Mademoiselle[897:20b]
/var/mobile/Applications/.../....app/GirlName.txt
error : Permission denied
Pouvez-vous me dire ce qui ne va pas?
La solution
Vous ne pouvez pas ouvrir un fichier dans le faisceau d'application et de le rendre accessible en écriture ( « w + »).
Ce même code fonctionnera probablement (je ne l'ai pas testé) si vous changez le mode « r »
Si vous avez besoin d'un fichier texte en écriture, vous devez placer dans un répertoire inscriptible, je ferais quelque chose comme ceci:
//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];
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow