Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
July 23, 2017, 03:42:02 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: How to: Program 16.6  (Read 3073 times)
clouded
Full Member
***
Posts: 123


« on: June 26, 2012, 11:44:20 AM »

For Program 16.6 the Output shown:

Quote
$ ls –l      see what files we have
total 96
-rwxr-xr-x 1 stevekoc staff 19956 Jul 24 14:33 copy
-rw-r--r-- 1 stevekoc staff  1484 Jul 24 14:32 copy.m
-rw-r--r-- 1 stevekoc staff  1403 Jul 24 13:00 file1.m
drwxr-xr-x 2 stevekoc staff    68 Jul 24 14:40 newdir
-rw-r--r-- 1 stevekoc staff  1567 Jul 24 14:12 path1.m
-rw-r--r-- 1 stevekoc staff    84 Jul 24 13:22 testfile
$ copy       try with no args
Usage: copy src dest
$ copy foo copy2
Can't read foo
$ copy copy.m backup.m
Copy of copy.m to backup.m succeeded!
$ diff copy.m backup.m      compare the files
$ copy copy.m newdir        try copy into directory
Copy of copy.m to newdir/copy.m succeeeded!
$ ls –l newdir
total 8
-rw-r—r— 1 stevekoc staff 1484 Jul 24 14:44 copy.m
$

is being worked from the Terminal application. There was a section in the book that explained briefly how to use it. (p. 17-19)

Before you can use this example, you must set up all the elements to get the correct outcome, otherwise you'll keep getting errors. I hope this helps.

1. Set-up the code in Xcode as such and save:
Code: (Objective-C)
//  main.m
//  Chapter 16 Program 6

// Implement a basic copy utility

#import <Foundation/Foundation.h>

int main (int argc, char *argv[])
{
    @autoreleasepool {
        NSFileManager   *fm;
        NSString        *source, *dest;
        BOOL            isDir;
        NSProcessInfo   *proc = [NSProcessInfo processInfo];
        NSArray         *args = [proc arguments];
        
        fm = [NSFileManager defaultManager];
        
        // Check for two arguments on the command line
        
        if ([args count] != 3) {
            NSLog(@"Usage: %@ src dest", [proc processName]);
            return 1;
        }
        
        source = [args objectAtIndex: 1];
        dest = [args objectAtIndex: 2];
        
        // Make sure the source file can be read
        
        if ([fm isReadableFileAtPath: source] == NO) {
            NSLog(@"Can't read %@", source);
            return 2;
        }
        
        // 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: [source lastPathComponent]];
        }
        
        // Remove the destination file if it already exists
        
        [fm removeItemAtPath: dest error: NULL];
        
        // Okay, time to perform the copy
        
        if ([fm copyItemAtPath: source toPath: dest error: NULL] == NO) {
            NSLog(@"Copy failed!");
            return  3;
        }
        
        NSLog(@"Copy of %@ to %@ succeeded!", source, dest);
    }
    return 0;
}

2. Make a copy of "main.m"... named "copy.m" (you should know the location of the file)

3. In the same folder as "copy.m" make a folder named "newdir"

4. Make note of the location of the file and folder.

5. Go to Finder and under Applications/Utilities/ open > Terminal

6. Go to folder location by typing "cd /location/of/the/Folder" OR "cd ./Folder" OR "cd ../.." OR "cd /location/of/the/Folder/'FolderName withSpaces useSingleQuote'", etc... use UNIX style commands and locations
**NOTE: Once there, the description to the left of the '$' changes to that folder location, check to verify you're in the right location (e.g. Cloudeds-MacBook-Pro:Chapter 16 Program 6 Clouded$ )

7. Verify the contents of your files by typing "ls -l"
At this point you should be able to see your copy.m file and the newfile folder on the list

8. Now you have to compile the file for use. Type "clang -fobjc-arc –framework Foundation copy.m -o copy", press enter/return, this is to compile using Terminal
Now you should have a compiled copy executable

9. Verify the contents of your files by typing "ls -l"
At this point you should be able to see your copy.m, copy file and the newfile folder on the list

10. To use your compiled file, just type './copy' and the files involved
Using "copy" as directed in the book's output example would mean your files are in your Documents folder... unless your system already knows where to find your files, the './' directs the compiler to the current directory, otherwise, it'll give you this message: -bash: copy: command not found

From here out you should be able to get around this Program.

Output:

Last login: Tue Jun 26 13:36:00 on ttys000
Cloudeds-MacBook-Pro:~ Clouded$ cd /Users/Clouded/Desktop/prog1/Chapter16/'Chapter 16 Program 6'/
Cloudeds-MacBook-Pro:Chapter 16 Program 6 Clouded$ ls -l
total 24
drwxr-xr-x  6 Clouded  staff   204 Jun 26 13:43 Chapter 16 Program 6
drwxr-xr-x  5 Clouded  staff   170 Jun 23 09:06 Chapter 16 Program 6.xcodeproj
-rw-r--r--@ 1 Clouded  staff  1880 Jun 26 13:44 copy.m
drwxr-xr-x  3 Clouded  staff   102 Jun 26 14:18 newdir
-rw-r--r--@ 1 Clouded  staff    83 Jun 22 20:19 testfile
Cloudeds-MacBook-Pro:Chapter 16 Program 6 Clouded$ clang -fobjc-arc –framework Foundation copy.m -o copy
Cloudeds-MacBook-Pro:Chapter 16 Program 6 Clouded$ ls -l
total 48
drwxr-xr-x  6 Clouded  staff   204 Jun 26 13:43 Chapter 16 Program 6
drwxr-xr-x  5 Clouded  staff   170 Jun 23 09:06 Chapter 16 Program 6.xcodeproj
-rwxr-xr-x  1 Clouded  staff  9096 Jun 26 14:31 copy
-rw-r--r--@ 1 Clouded  staff  1880 Jun 26 13:44 copy.m
drwxr-xr-x  3 Clouded  staff   102 Jun 26 14:18 newdir
-rw-r--r--@ 1 Clouded  staff    83 Jun 22 20:19 testfile
Cloudeds-MacBook-Pro:Chapter 16 Program 6 Clouded$ copy
-bash: copy: command not found
Cloudeds-MacBook-Pro:Chapter 16 Program 6 Clouded$ ./copy
2012-06-26 14:38:17.892 copy[3465:707] Usage: copy src dest
Cloudeds-MacBook-Pro:Chapter 16 Program 6 Clouded$ ./copy foo copy2
2012-06-26 14:53:16.342 copy[3486:707] Can't read foo
Cloudeds-MacBook-Pro:Chapter 16 Program 6 Clouded$ ./copy copy.m backup.m
2012-06-26 14:53:43.916 copy[3489:707] Copy of copy.m to backup.m succeeded!
Cloudeds-MacBook-Pro:Chapter 16 Program 6 Clouded$ diff copy.m backup.m
Cloudeds-MacBook-Pro:Chapter 16 Program 6 Clouded$ ./copy copy.m newdir
2012-06-26 14:54:35.571 copy[3494:707] Copy of copy.m to newdir/copy.m succeeded!
Cloudeds-MacBook-Pro:Chapter 16 Program 6 Clouded$ ls -l newdir
total 16
-rw-r--r--@ 1 Clouded  staff  1880 Jun 26 13:44 copy.m
Cloudeds-MacBook-Pro:Chapter 16 Program 6 Clouded$ exit


***Don't forget to delete the files you create... the system won't automatically do it.***
« Last Edit: June 26, 2012, 02:35:42 PM by clouded » Logged
symanski
Newbie
*
Posts: 4


« Reply #1 on: July 04, 2012, 06:15:42 AM »

If you've done the previous examples you should know how to find where the program is executed.   You can then go to that location using the Finder window, as it's much easier that way.

Start up a terminal window, but this will be in the wrong location.   Looking at the Finder window, "Get Info" on the file and you'll see the whole location given.  Select and copy this location, back to the terminal window and "cd " (note the space) and past in the path.

Create a file and a directory in this location.   You can copy the file you created in other programs over and then rename it if you want.

I called my program "Prog-16-6" so to run it I did "./Prog-16-6 test.m testdir"   The ./ tells the machine that the program to run is in the local directory.   The two following of "test.m" and "testdir" are my source test file and destination directory.   From the Finder window you'll be able to see the new file created.

Logged
MSchmidleitner
Newbie
*
Posts: 4


« Reply #2 on: July 12, 2012, 07:26:26 AM »

Hi, clouded!
I followed your instruction, but I get following error message on the terminal:

/*
Michaelas-MacBook-Pro:desktop michaela$ cd test
Michaelas-MacBook-Pro:test michaela$ ls -l
total 8
-rw-r--r--@ 1 michaela  staff  1684 12 Jul 15:40 copy.m
drwxr-xr-x  2 michaela  staff    68 12 Jul 16:02 newdir
Michaelas-MacBook-Pro:test michaela$ clang -fobjc-arc –framework Foundation copy.m -o copy
clang: error: no such file or directory: '–framework'
clang: error: no such file or directory: 'Foundation'
*/

Any idea, why?
Logged
andreasschaffner
Newbie
*
Posts: 1


Email
« Reply #3 on: September 04, 2012, 12:55:12 PM »

I had the same problem as you Schmidleitner that it did not work immediately with the following command:

clang -fobjc-arc -framework Foundation copy.m -o copy

best thing is to type it in and then you get there. What I was struggling with is that the command clang was not installed on my Mac terminal. I did some research and you have to go to Xcode Preferences Downloads Components and install Command Line Tools. Otherwise the compiler clang cannot be started in the terminal. Stephen your book is without question an excellent resource. The file section at the moment is the only one I struggle to get the programs running. Probably it would be helpful if you could take the newbee a bit more by the hand.  Cheesy
Logged
cary_W
Newbie
*
Posts: 10


Email
« Reply #4 on: May 31, 2016, 10:46:34 PM »

In the 6th version, you should type"lang -fobjc-arc copy.m -o copy" to compile in the Terminal.
Logged
LavrinPristazh
Newbie
*
Posts: 1


Email
« Reply #5 on: April 19, 2017, 01:15:07 AM »

In the 7th version of Xcode, you should type"clang -fobjc-arc copy.m -o copy" to compile in the Terminal.
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.