Task

Load the entire contents of some text file as a single string variable.

If applicable, discuss: encoding selection, the possibility of memory-mapping.

Of course, in practice one should avoid reading an entire file at once
if the file is large and the task can be accomplished incrementally instead
(in which case check File IO);
this is for those cases where having the entire file is actually what is wanted.




 
    /*** 0. PREPARATION    */
    // We need a text file to read; let's redirect a C string to a new file
    // using the shell by way of the stdlib system() function.
    system ("echo \"Hello, World!\" > ~/HelloRosetta");
 
 
 
    /*** 1. THE TASK      */
    // Instantiate an NSString which describes the filesystem location of
    // the file we will be reading.
    NSString *filePath = [NSHomeDirectory() stringByAppendingPathComponent:@"HelloRosetta"];
 
    // The selector we're going to use to complete this task,
    // stringWithContentsOfFile:encoding:error, has an optional `error'
    // parameter which can be used to return information about any
    // errors it might run into. It's optional, but we'll create an NSError
    // anyways to demonstrate best practice.
    NSError *anError;
 
    // And finally, the task: read and store the contents of a file as an
    // NSString.
    NSString *aString = [NSString stringWithContentsOfFile:filePath
                                                  encoding:NSUTF8StringEncoding
                                                     error:&anError];
 
    // If the file read was unsuccessful, display the error description.
    // Otherwise, display the NSString.
    if (!aString) {
        NSLog(@"%@", [anError localizedDescription]);
    } else {
        NSLog(@"%@", aString);
    }
 