getting an error when trying to use seekToFileOffset
I am currently trying to read a line of 5 characters from a offset in my text file. I am pretty sure everything is working however when I print the contence of my buffer to the log, it outputs this <7466315c 61>
- (void)fetchCode:(id)sender{
NSData *databuffer;
NSString *path = [[NSBundle mainBundle] pathForResource:@"nCode01" ofType:@"txt"];
nCode = [NSFileHandle fileHandleForReadingAtPath:path];
if (nCode == nil) {
NSLog (@"Open of nCode for reading failed\n");
}
[nCode seekToFileOf开发者_Python百科fset: 3];
databuffer = [nCode readDataOfLength: 5];
NSLog (@"Data = %@", databuffer);
[nCode closeFile];
}
I think it might be a format error, not a memory as each time I run the method it prints the same <7466315c 61> any idea of what I am missing / doing wrong?
NSData prints its bytes as hex numbers, in groups of 4 bytes. <7466315c 61>
corresponds to the 5 characters "tf1\a". You could use NSString's initWithData:encoding:
to convert it to an NSString, if necessary, or you could access the NSData's bytes
and interpret them as a (possibly not terminated) C-style string.
What exactly are you expecting to have read?
精彩评论