working on a program from a book by Stephen G. Kochan
here is the program which i called prog16.5.m
here is the output from the book:
and the output i got
the program seemed to compile and run ok.
just wondering about the logic behind this program like,
in the program the lines where it says:
i thought of replacing @"~stevekochan with my home directory like,
@"~jamescollins
also the temporary dir, is this a directory that the program created?
why didn't my program create the temporary dir in /Tmp?
any help would be appreciated.
here is the program which i called prog16.5.m
Code:
// some basic path operations
#import <Foundation/NSString.h>
#import <Foundation/NSArray.h>
#import <Foundation/NSFileManager.h>
#import <Foundation/NSAutoreleasePool.h>
#import <Foundation/NSPathUtilities.h>
#import <stdio.h>
int main (int argc, char *argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSString *fName = @"path.m";
NSFileManager *NSFm;
NSString *path, *tempdir, *extension, *homedir, *fullpath;
NSString *upath = @"~stevekochan/progs/../ch16/./path.m";
NSArray *components;
int i, n;
NSFm = [NSFileManager defaultManager];
//get the temporary working directory
tempdir = NSTemporaryDirectory ();
printf ("Temporary Directory is %s\n", [tempdir UTF8String]);
// extract the base directory from current directory
path = [NSFm currentDirectoryPath];
printf ("Base dir is %s\n", [[path lastPathComponent] UTF8String]);
//create a full path to the file fName in current directory
fullpath = [path stringByAppendingPathComponent: fName];
printf ("fullpath to %s is %s\n", [fName UTF8String], [fullpath UTF8String]);
// get the file name extension
extension = [fullpath pathExtension];
printf ("extension for %s is %s\n", [fullpath UTF8String],
[extension UTF8String]);
// get user's home directory
homedir = NSHomeDirectory ();
printf ("Your home directory is %s\n", [homedir UTF8String]);
// divide a path into its components
components = [homedir pathComponents];
n = [components count];
for (i = 0; i < n; ++i)
printf (" %s\n", [[components objectAtIndex: i] UTF8String]);
// "standardize" a path
printf ("%s => %s\n", [upath UTF8String],
[[upath stringByStandardizingPath] UTF8String]);
[pool release];
return 0;
}
here is the output from the book:
Code:
Temporary Directory is /tmp
Base dir is ch16
fullpath to path.m is /Users/stevekochan/mysrc/ch16/path.m
extension for /Users/stevekochan/mysrc/ch16/path.m is m
Your home directory is /Users/stevekochan
/
Users
stevekochan
~stevekochan/progs/../ch16/./path.m => /Users/stevekochan/ch16/path.m
and the output i got
Code:
james-collinss-macbook-pro:prog16 jamescollins$ ./prog16.5
Temporary Directory is /var/folders/Sh/ShzETZejFJOmnWAK-et4WE+++TI/-Tmp-/
Base dir is prog16
fullpath to path.m is /Users/jamescollins/objcee/prog16/path.m
extension for /Users/jamescollins/objcee/prog16/path.m is m
Your home directory is /Users/jamescollins
/
Users
jamescollins
~stevekochan/progs/../ch16/./path.m => ~stevekochan/ch16/path.m
the program seemed to compile and run ok.
just wondering about the logic behind this program like,
in the program the lines where it says:
Code:
NSString *upath = @"~stevekochan/progs/../ch16/./path.m";
i thought of replacing @"~stevekochan with my home directory like,
@"~jamescollins
also the temporary dir, is this a directory that the program created?
why didn't my program create the temporary dir in /Tmp?
any help would be appreciated.