Amazon.com Widgets Attempted answer to exercise 16-6 (NSLog version )---with a question!
Welcome, Guest. Please login or register.
Did you miss your activation email?
November 24, 2014, 06:45:16 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)
| | | |-+ Attempted answer to exercise 16-6 (NSLog version )---with a question!
Pages: [1] Go Down
Print
Author Topic: Attempted answer to exercise 16-6 (NSLog version )---with a question! (Read 894 times)
mdeh
Full Member
***
Posts: 166






on: February 19, 2009, 01:45:36 PM

I used a do loop to iterate the buffer, but needed to check  **intra** loop to see if the process was complete. What is the **best** way to do this?
My approach seems clunky.
Thanks.

PS...have attached a file to add to the current working directory for those who wish to test the program with something real. Not zipped...

Code: (Objective-C)
#import <Foundation/NSObject.h>
#import <Foundation/NSString.h>
#import <Foundation/NSFileManager.h>
#import <Foundation/NSFileHandle.h>
#import <Foundation/NSAutoreleasePool.h>
#import <Foundation/NSData.h>

#define kBufSize 8
#define ZERO 0
#define BUFFER_NOT_EMPTY 1




int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSFileManager *fm = [ NSFileManager defaultManager];
NSString *srcfile = @"Gettysburg_address", *destfile = @"testout";
NSData *buffer;
NSFileHandle *infile, *outfile;



/* open source file */

infile = [NSFileHandle fileHandleForReadingAtPath: srcfile];

if ( infile == nil)
{
NSLog(@"Error: Open of source file failed");
return 1;
}

/*create destination file */

[fm createFileAtPath:destfile contents:nil attributes:nil];

if  ( ! [ fm fileExistsAtPath: destfile] )
{
NSLog(@"Error: Cannot create destination file");
return 2;
}

/* open destination file for writing */

outfile = [ NSFileHandle fileHandleForWritingAtPath: destfile];

if ( outfile == nil)
{
NSLog(@"Error: Cannot write to destination file");
return 3;
}

do {


(buffer = [infile readDataOfLength: kBufSize]);
if ( buffer.length == ZERO)
break;
NSLog(@"Written:  %i bytes", buffer.length);
[outfile writeData: buffer];

}while(BUFFER_NOT_EMPTY);

/* show copied file */

NSLog(@"%@", [ NSString stringWithContentsOfFile: destfile encoding: NSUTF8StringEncoding error: nil]);

    [pool drain];
    return 0;
}

http://[img][/img]


I have been thinking about that clunky do loop. How about this for a replacement?
Code: (Objective-C)
	/* initialize buffer */

(buffer = [infile readDataOfLength: kBufSize]);

while (buffer.length != ZERO) {
[outfile writeData: buffer];
NSLog(@"Written:  %i bytes", buffer.length);
(buffer = [infile readDataOfLength: kBufSize]);
};

Last Edit: February 20, 2009, 10:49:39 AM by mdeh Logged
skochan
Administrator
Hero Member
*****
Posts: 3114







Reply #1 on: February 22, 2009, 05:20:29 AM

The while loop works for me Michael.

Cheers,

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