For Program 16.6 the Output shown:
$ 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:
// 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 list8. 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 executable9. 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 list10. 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 foundFrom 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.***