Changes

Working with Files in Objective-C

3 bytes added, 16:32, 11 November 2009
NSFileHandle File Offsets and Seeking
== NSFileHandle File Offsets and Seeking ==
NSFileHandle objects maintain a pointer to the current position in a file. This is referred to as the ''offset''. When a file is first opened the offset is set to 0. This means that any read or write operations we perform using the NSFileHandle methods will take place at offset 0 in the file. To perform operations at different locations in a file (for example to append data to the end of the file) it is first necessary to ''seek'' to the required offset. For example to move the current offset to the end of the file, use the ''seekToEndOfFile'' method. Alternatively, ''seekToFileOffset'' allows you to specifiy the precise location in the file to whichthe which the offset is to be positionpositioned. Finally, the current offset may be identified using the ''offsetInFile'' method. in order to accommodate large files, the offset is stored in the form of an unsigned long long.
The following example opens a file for reading and then performs a number of method calls to move the offset to different positions, outputting the current offset after each move: