Amazon.com Widgets Program 16.1
Welcome, Guest. Please login or register.
Did you miss your activation email?
September 02, 2014, 02:17:09 PM
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
|-+ Old Stuff
| |-+ Program Examples
| | |-+ Chapter 16
| | | |-+ Program 16.1
Pages: [1] Go Down
Print
Author Topic: Program 16.1 (Read 1772 times)
TotalLuck
Full Member
***
Posts: 107



WWW Email




on: February 21, 2009, 02:36:37 PM


Code: (Objective-C)
// Basic File operations  
// Assumes he existence os a file called "testfile"  
// in the current directory  


#import <Foundation/Foundation.h>  

int main (int argc, const char * argv[]) {  
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];  

//   **************  16.1  

    NSString            *fName = @"testfile";  
    NSFileManager       *fm;  
    NSDictionary        *attr;  

    // Need to create an instance of the  file manager  

    fm = [NSFileManager defaultManager];  

    // 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");  

    // Dsiplay the contents of  the newly-created file  

    NSLog(@"%@", [NSString stringWithContentsOfFile: @"newfile2"
encoding: NSUTF8StringEncoding error: NULL]);  

    [pool drain];  
    return 0;  
}

[/pre]
Last Edit: July 28, 2011, 10:34:51 PM by skochan Logged

Apps available on  iTunes store:
"ADACode"  iPhone and iPad versions, "ADAGuidelines" iPhone & iPad versions,  "Rehabilitation Act of 1973" for iPhone
"APokerTimer" now for iPhone http://bit.ly/h1fAJp
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.