Amazon.com Widgets Attempt at exercise 5
Welcome, Guest. Please login or register.
Did you miss your activation email?
October 31, 2014, 02:40:12 AM
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
| |-+ Answers to Exercises
| | |-+ Chapter 16 (Moderator: sgg_admin)
| | | |-+ Attempt at exercise 5
Pages: [1] Go Down
Print
Author Topic: Attempt at exercise 5 (Read 1178 times)
esc
Global Moderator
Full Member
*****
Posts: 230






on: February 19, 2009, 10:00:23 AM

#import <Foundation/Foundation.h>


@interface NSString (TempFiles)
- (NSString *) temporaryFileName: (NSString *) tf;
@end

@implementation NSString (TempFiles)
- (NSString *) temporaryFileName: (NSString *) tmpfile
{
   NSProcessInfo *proc = [NSProcessInfo processInfo];
   NSString *uniquestr = [proc globallyUniqueString];
   
   return [tmpfile stringByAppendingPathComponent:uniquestr];
}
@end

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
   
   char *template = malloc(sizeof(char) * 128);
   strcpy(template, "/tmp/temp.XXXXXXXX");
   
   NSString *tmpfilename = NSTemporaryDirectory();

    printf("Unique file name by Foundation methods =\n");
   printf("%s\n", [[tmpfilename temporaryFileName:tmpfilename] UTF8String]);
   
   mkstemp(template);
   printf("Unique file name by mkstemp = %s\n", template);
   
    [pool drain];
    return 0;
}

Sample output:

Unique file name by Foundation methods =
/var/folders/HB/HBtCxZ2SEbuSnIsdX70KCU+++TM/-Tmp-/0395F071-1DDD-4AAF-B591-F1F18C24A1D6-19934-0000E5893FD9445B
Unique file name by mkstemp = /tmp/temp.aiNyOFRN


Logged
mdeh
Full Member
***
Posts: 166






Reply #1 on: February 19, 2009, 11:34:23 AM

Wendy...we are working on the same program!
But..you are doing it in C...which is great...but not sure if malloc, strcpy are necessarily the methods that non-C are familiar with and within scope of the exercise....
Having said that...I cannot get mine to work.....      Sad    ..so maybe some help?
Found the error.....don't even ask!!!!


TempFiles.h
Code: (Objective-C)
#import <Foundation/Foundation.h>


@interface NSString(TempFiles)

+(NSString *) temporaryFileName;

@end

TempFiles.m
Code: (Objective-C)
#import "TempFiles.h"


@implementation NSString (TempFiles)


+(NSString *) temporaryFileName
{
NSString * tempDir = NSTemporaryDirectory();
NSString * uniqueFileName = [[NSProcessInfo processInfo]globallyUniqueString];
return [tempDir stringByAppendingPathComponent: uniqueFileName];
}

@end


main.m
Code: (Objective-C)
#import "TempFiles.h"
#define NUM_OF_STRINGS  10

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSMutableArray *myStrings = [[NSMutableArray alloc]init];
NSFileManager *fm = [ NSFileManager defaultManager];
BOOL done = NO;



int x;


for ( x = 0; x < NUM_OF_STRINGS; x ++)
{
[myStrings addObject:[NSString temporaryFileName]];


/* create a file at that location */

done = [fm createFileAtPath:[myStrings lastObject] contents:nil attributes:nil];

if ( !done)
{
NSLog(@"Could not create file");
return 1;
}


}

/* show unique names */

for ( id unique_show in myStrings)
NSLog(@"%@", unique_show);

/*now delete those from temporary folder*/

for ( id unique_delete in myStrings)
{

done = [fm removeFileAtPath: unique_delete handler: nil];
if (!done)
NSLog(@"Did not delete file: %@", unique_delete);
else
NSLog(@"Deleted: %@", unique_delete);

}
    [pool drain];
    return 0;
}







Last Edit: February 19, 2009, 12:05:36 PM by mdeh Logged
esc
Global Moderator
Full Member
*****
Posts: 230






Reply #2 on: February 19, 2009, 12:23:14 PM

But..you are doing it in C...which is great...but not sure if malloc, strcpy are necessarily the methods that non-C are familiar with and within scope of the exercise....

oops, my use of malloc and strcpy was quick and dirty for calling mkstemp()...I'll go back to do it within Objective-C.  Thanks for pointing this out!


Logged
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.