0

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 seekToFileOffset: 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?

tinhead
  • 385
  • 2
  • 10
  • 29

1 Answers1

1

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?

Anomie
  • 92,546
  • 13
  • 126
  • 145
  • however for the time being I am just experimenting with a plane textfile, just to see how this offsetfunction workes... I just tried this but not sure how to initialize the string 'NSString *newBuffer = [[NSString alloc] initWithFormat:[nissanCode readDataOfLength:2]];' – tinhead Jun 15 '11 at 01:22
  • @tinhead: I meant "What text are you expecting to have read from the file when you got `<7466315c 61>` instead?". – Anomie Jun 15 '11 at 10:46
  • @tinhead: `initWithFormat:` takes a printf-style format string. Not an NSData object. Read [the documentation](http://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html) for details. – Anomie Jun 15 '11 at 10:47