Amazon.com Widgets Chapter 16 Program 1
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 24, 2013, 03:06:06 AM
Home Help Search chat Login Register   
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan
                     

+  Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+  Programming in Objective-C, 4th edition
| |-+  Chapter 16
| | |-+  Chapter 16 Program 1
Pages: [1]   Go Down
Print
Author Topic: Chapter 16 Program 1  (Read 390 times)
clouded
Full Member
***
Posts: 123






« on: June 25, 2012, 12:14:40 PM »

You must create our own testfile in Xcode using the author's note, then copy and paste it OR use it in it's current directory by putting the full path to the file

Code: (Objective-C)
//  Chapter 16 Program 1

// Basic File operations
// Assumes the existence of a file called "testfile"
// in the current directory

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{
   
    @autoreleasepool {
       
        NSString *fName = @"testfile"; // or put full address path (e.g. @"/User/TestSubject/testfile")
        NSFileManager *fm;
        NSDictionary *attr;
       
        // Need to create an instance of the file manager
        fm = [NSFileManager defaultManager];
       
        // Can place testfile in Debug directory instead of full address
        // To find the current directory uncomment the following lines
        //NSString *path;
        //path = [fm currentDirectoryPath];
        //NSLog (@"Current directory path is %@", path);
       
        // Let's make sure our test file exists first
        if ([fm fileExistsAtPath: fName] == NO) {
            NSLog(@"File doesn't exist!");
            return 1;
        }
       
        //now lets make a copy
        if ([fm copyItemAtPath: fName toPath: @"newfile" error: NULL] == NO) {
            NSLog(@"File Copy failed!");
            return 2;
        }

        // Now let's test to see if the two files are equal
        if ([fm contentsEqualAtPath: fName andPath: @"newfile"] == NO) {
            NSLog(@"Files are Not Equal!");
            return 3;
        }
       
        // Now lets rename the copy
        if ([fm moveItemAtPath: @"newfile" toPath: @"newfile2" error: NULL] == NO){
            NSLog(@"File rename Failed");
            return 4; }
       
        // get the size of the newfile2
        if ((attr = [fm attributesOfItemAtPath: @"newfile2" error: NULL]) == nil) {
            NSLog(@"Couldn't get file attributes!");
            return 5;
        }
        NSLog(@"File size is %llu bytes", [[attr objectForKey: NSFileSize] unsignedLongLongValue]);
       
        // And finally, let's delete the original file
        if ([fm removeItemAtPath: fName error: NULL] == NO) {
            NSLog(@"file removal failed");
            return 6;
        }
        NSLog(@"All operations were successful");
       
        // Display the contents of the newly-created file
        NSLog(@"%@", [NSString stringWithContentsOfFile: @"newfile2" encoding:NSUTF8StringEncoding error:NULL]);
    }
    return 0;
}
Logged
Pages: [1]   Go Up
Print
Jump to:  



Login with username, password and session length

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents (c) 2009 classroomM.com. All rights reserved.