Append data to file in iPhone app
Posted
by zp26
on Stack Overflow
See other posts from Stack Overflow
or by zp26
Published on 2010-05-31T13:57:47Z
Indexed on
2010/05/31
14:03 UTC
Read the original article
Hit count: 373
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);
}
© Stack Overflow or respective owner