I have tested my app on simulators 4.3, 5.0, 5.1 and 6.0 for iPod, iPad an iPhone. It's working as expected. But on all my devices it doesn't work.
I know that on devices the filenames are case-sensitive. Both the used constant DATA_FILE and the type are lowercase, and the file handle is not nil after opening. As I can see, there is no actual reading after the proper file positioning. Why not?
This is the code, reduced to the minimum to show the problem.
NSLog(@"getData, index = %lu", index);
NSFileHandle *fhIndex = [NSFileHandle fileHandleForReadingAtPath: [[NSBundle mainBundle] pathForResource: DATA_FILE ofType:@"idx"]];
u_char indexData[4];
NSLog(@"index*4 = %lu", (index * 4));
[fhIndex seekToFileOffset: (index * 4)];
unsigned long long test = [fhIndex offsetInFile];
NSLog(@"FilePos = %llu",test);
[[fhIndex readDataOfLength: 4] getBytes: indexData length: 4];
test = [fhIndex offsetInFile];
NSLog(@"FilePos = %llu",test);
NSLog(@"indexData = %i %i %i %i", indexData[0], indexData[1], indexData[2], indexData[3]);
An index (unsigned long) is given to the routine, in this case index=62825. The program make a seek to position index*4 and then reads the next four bytes. On devices, these four bytes are not read!
Debugger output in the simulator:
2012-10-31 10:46:24.557 program[469:12003] getData, index = 62825
2012-10-31 10:46:24.559 program[469:12003] index*4 = 251300
2012-10-31 10:46:24.559 program[469:12003] FilePos = 251300
2012-10-31 10:46:24.580 program[469:12003] FilePos = 251304
2012-10-31 10:46:24.581 program[469:12003] indexData = 0 73 130 174
... and on the devices:
2012-10-31 10:41:57.776 program[2942:907] getData, index = 62825
2012-10-31 10:41:57.780 program[2942:907] index*4 = 251300
2012-10-31 10:41:57.782 program[2942:907] FilePos = 251300
2012-10-31 10:41:57.783 program[2942:907] FilePos = 251300
2012-10-31 10:41:57.784 program[2942:907] indexData = 248 10 18 0
As you can see in the output on devices, the file position is not changed after the read, that means, there was actually no read!