Amazon.com Widgets exercise 16-6
Welcome, Guest. Please login or register.
Did you miss your activation email?
June 20, 2013, 03:31:38 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)
| | | |-+  exercise 16-6
Pages: [1]   Go Down
Print
Author Topic: exercise 16-6  (Read 682 times)
mdziedzic
Newbie
*
Posts: 41



WWW




« on: December 15, 2009, 12:08:51 PM »

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

#define kBufSize 128

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

NSFileHandle *inFile, *outFile;
NSData *buffer;

// open the file testfile for reading

inFile = [NSFileHandle fileHandleForReadingAtPath: @"../../testfile"];

if (inFile == nil) {
NSLog(@"Open of testfile for reading failed!");
return 1;
}

// create the output file first if necessary

[[NSFileManager defaultManager] createFileAtPath: @"../../testout" contents: nil attributes: nil];

// now open the file for writing

outFile = [NSFileHandle fileHandleForWritingAtPath: @"../../testout"];

if (outFile == nil) {
NSLog(@"Open of testout for writing failed!");
return 2;
}

// truncate the output file since it may contain data

[outFile truncateFileAtOffset: 0];

// read the data from inFile and write it to outFile in kBufSize chunks

while ([buffer = [inFile readDataOfLength: kBufSize] bytes] != nil)
[outFile writeData: buffer];

// close the two files

[inFile closeFile];
[outFile closeFile];

// verify the file's contents

NSLog(@"%@", [NSString stringWithContentsOfFile: @"../../testout" encoding: NSUTF8StringEncoding
  error: nil]);
   
[pool drain];
    return 0;
}
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.