Append data to file in iPhone app
- by zp26
I have a problem.
I want to create a file like XML.
I have create a code for this but the file not append the information but rewrite and save only the last NSData.
Can you help me/
This is my code
-(void)salvataggioInXML:(NSString*)name:(float)x:(float)y:(float)z{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectoryPath = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectoryPath stringByAppendingPathComponent:@"filePosizioni.xml"];
NSFileHandle *myHandle = [NSFileHandle fileHandleForUpdatingAtPath:filePath];
[myHandle seekToEndOfFile];
NSString *tagName = [NSString stringWithFormat:@"<name>%@<name>", name];
NSString *tagX = [NSString stringWithFormat:@"<x>%f<x>", name];
NSString *tagY = [NSString stringWithFormat:@"<y>%f<y>", name];
NSString *tagZ = [NSString stringWithFormat:@"<z>%f<z>", name];
NSData* dataName = [tagName dataUsingEncoding: NSASCIIStringEncoding];
NSData* dataX = [tagX dataUsingEncoding: NSASCIIStringEncoding];
NSData* dataY = [tagY dataUsingEncoding: NSASCIIStringEncoding];
NSData* dataZ = [tagZ dataUsingEncoding: NSASCIIStringEncoding];
if ([dataName writeToFile:filePath atomically:YES])
NSLog(@"writeok");
[myHandle seekToEndOfFile];
if ([dataX writeToFile:filePath atomically:YES])
NSLog(@"writeok");
[myHandle seekToEndOfFile];
if ([dataY writeToFile:filePath atomically:YES])
NSLog(@"writeok");
[myHandle seekToEndOfFile];
if ([dataZ writeToFile:filePath atomically:YES])
NSLog(@"writeok");
[myHandle seekToEndOfFile];
NSLog(@"zp26 %@",filePath);
}