Write at the beginning of a file
I have a fonction who write in a txt file, but at the end of the file, I want a know how to write at the beginning of the file.
this is my fonction:
-(void)ecrireLog:(NSString *) identificateur
{
NSFileHandle *fichier;
NSData *data ;
NSString *chaineLog;
if ([self creerFichier])
{
chaineLog = [FonctionUtile concatener:[FonctionUtile dateDuJour] chaine2:@" " chaine3:[FonctionUtile heureCourante] chaine4:@" : "];
chaineLog = [FonctionUtile concatener:chaineLog chaine2:@" " chaine3:identificateur chaine4:@"\n"];
data = [chaineLog dataUsingEncoding:NSUTF8StringEncoding];
fichier = [NSFileHandle fileHandleForUpdatingAtPath:cheminFichier];
if (fichier == nil)
{
NSLog(@"Erreur dans l'ouverture du fichier");
}
else
{
[fichier seekToEndO开发者_StackOverflow中文版fFile];
[fichier writeData: data];
}
[fichier closeFile];
}
}
thx
Somehow I think this is a trick question.
Don't call seekToEndOfFile?
What if you loaded the text file into an NSString, then created a new NSString with whatever text you want in the beginning, then concatenated the two strings together and saved as a text file? Seems simple enough.
精彩评论