Welcome, Guest. Please login or register.
Did you miss your activation email?
May 23, 2017, 09:48: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)
| | | |-+ Attempted answer to exercise 16-6 (NSLog version )---with a question!
Pages: [1] Go Down
Author Topic: Attempted answer to exercise 16-6 (NSLog version )---with a question! (Read 1294 times)
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.

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

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)
NSLog(@"Written:  %i bytes", buffer.length);
[outfile writeData: buffer];


/* show copied file */

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

    [pool drain];
    return 0;


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
Hero Member
Posts: 3114

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

The while loop works for me Michael.


Pages: [1] Go Up
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.