Changes

Working with Files in Objective-C

7 bytes added, 20:17, 11 May 2010
no edit summary
== Checking if a File is Readable/Writable/Executable/Deletable ==
Most operating systems provide some level of file access control. These typically take the form of attributes that control the level of access to a file for each user or user group. As such, it is not a certainty that your program will have read or write access to a particular file, or the appropriate permissions to delete or execute it. The quickest way to find out if your program has a particular access permission is to use the ''isReadableFileAtPath'', ''isWritableFileAtPath'', ''isExecutableFileAtPath'' and ''isDeletableFileAtPath'' methods. Each method takes a single argument in the form of the path to the file to be checked and returns a boolean YES or NO result. For example, the following code excerpt checks to find out if a file is writable:
<pre>
== Moving/Renaming a File ==
A file may be renamed (assuming adequate permissions) using the ''moveItemAtPath'' method. This method returns a boolean YES or NO result and takes as arguments the pathname for the file to be moved, the destination path and an optional NSError object into which information describing any errors encountered during the operation will be placed. If no error description information is required, this argument may be set to NULL. Note that if the destination file path already exists this operation will fail.
<pre>
== Creating an NSFileHandle Object ==
An NSFileHandle object can be created when opening a file for reading, writing or updating (reading and writing). This is achieved using the ''fileHandleForReadingAtPath'', ''fileHandleForWritingAtPath'' and ''fileHandleForUpdatingAtPath'' methods respectively. Having opened a file, it must subsequently be closed when we have finsihed finished working with it using the ''closeFile'' method. If an attempt to open a file fails, for example because an attempt is made to open a non-existent file for reading, these methods return ''nil''.
For example, the following code excerpt opens a file for reading and writing and then closes it without actually doing anything to the file:
== 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 (the beginning of the file). 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 specify the precise location in the file to which the offset is to be positioned. 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: