NSData 小测试

 

 NSString *path = @"/Users/Wang/Desktop/test.dat";
  NSString *temp = @"Allen Dang";
 int i = 100;
 float f = 98.3333f;
 
 NSMutableData *writer = [[NSMutableData alloc] init];
  [writer appendData:[temp dataUsingEncoding:NSUTF8StringEncoding]];
 [writer appendBytes:&i length:sizeof(i)];
 [writer appendBytes:&f length:sizeof(f)];
  [writer writeToFile:path atomically:YES];
 [writer release];

 int ii;
 float ff;
 NSString *ttemp;
 NSData *reader = [NSData dataWithContentsOfFile:path];
 ttemp = [[NSString alloc] initWithData:[reader subdataWithRange:NSMakeRange(0, [temp length])]
          encoding:NSUTF8StringEncoding];
 [reader getBytes:&ii range:NSMakeRange([temp length], sizeof(ii))];
 [reader getBytes:&ff range:NSMakeRange([temp length] + sizeof(ii), sizeof(ff))];
  
 NSLog(@"string:%@  int:%i float:%f", ttemp, ii, ff);

 

转载:http://sue602.blog.163.com/blog/static/314953072009929115356149/
 
 
 [temp release];
 [ttemp release];
 [path release];

阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭