Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
December 16, 2017, 12:09:44 AM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
 
   Home   Help Search Login Register Chat  
Pages: [1]   Go Down
  Print  
Author Topic: exercise1(three argument version)  (Read 1658 times)
cary_W
Newbie
*
Posts: 10


Email
« on: June 02, 2016, 06:49:19 PM »

Code: (Objective-C)
//
//  main.m
//  prog16.6
//
//  Created by cw on 6/1/16.
//  Copyright 2016 Breeze. All rights reserved.
//

// Implement a basic copy utility
#import <Foundation/Foundation.h>
int main (int argc, char *argv[])
 {
    @autoreleasepool {
        NSFileManager *fm;
        NSString *source1, *dest, *source2;
        BOOL isDir;
        NSProcessInfo  *proc = [NSProcessInfo processInfo];
        NSArray *args = [proc arguments];
        fm = [NSFileManager defaultManager];
        // Check for two arguments on the command line
        if ([args count] != 4) {
            NSLog (@"Usage: %@ src dest", [proc processName]);
            return 1;
        }
        source1 = args[1];
        //add one more argument
        source2 = args[2];
        dest = args[3];
        // Make sure the source file can be read
        if ([fm isReadableFileAtPath: source1] == NO) {
            NSLog (@"Can't read %@", source1);
            return 2;
        }
        if ([fm isReadableFileAtPath: source2] == NO) {
            NSLog (@"Can't read %@", source2);
            return 3;
        }
        // See if the destination file is a directory
        // if it is, add the source to the end of the destination
        [fm fileExistsAtPath: dest isDirectory: &isDir];
        if (isDir == YES)
        {
            dest = [dest stringByAppendingPathComponent:[source1 lastPathComponent]];
            //copy the source2 to the destination
           // dest = [dest stringByAppendingPathComponent:[source2 lastPathComponent]];
        }
        // Remove the destination file if it already exists
        [fm removeItemAtPath: dest error: NULL];
        // Okay, time to perform the copy
        if ([fm copyItemAtPath: source1 toPath: dest error: NULL] == NO)
        {
            NSLog (@"Copy the first file failed!");
            return 4;
        }
        //if the first copy succeed, then change the path to source2
        else
            dest = [[dest stringByDeletingLastPathComponent]stringByAppendingPathComponent:source2];
        // Remove the destination file if it already exists
        [fm removeItemAtPath: dest error: NULL];
        //perform the copy
        if ([fm copyItemAtPath: source2 toPath: dest error: NULL] == NO)
        {
            NSLog (@"Copy the second file failed!");
            return 5;
        }
        NSLog (@"Copy of %@ and %@ to %@ succeeded!", source1,source2, dest); }
     return 0;
 }

then do the following things:
1.copy the main.m file to the Desktop(easy to have the work done)
2.open terminal
2.5.change the directory to Desktop using"cd Desktop/"
3.using "mv main.m copy.m" to rename the main.m file
4.compile the copy.m file using command"clang -fobjc-arc copy.m -o copy"
5.perform the copy using "./copy text1 text2 progs/" (make sure you create text1,text2 file and progs folder in the desktop"
Logged
Pages: [1]   Go Up
  Print  
 
Jump to:  

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 ゥ 2009 classroomM.com. All rights reserved.