Welcome, Guest. Please login or register.
Did you miss your activation email?
July 29, 2016, 03:10:37 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 6 and question
Pages: [1] Go Down
Author Topic: Attempt at exercise 6 and question (Read 1313 times)
Global Moderator
Full Member
Posts: 230

on: February 19, 2009, 10:26:47 AM

Please read my explanation and question after the code.

#import <Foundation/Foundation.h>

// Test with foo not exist and bar already exist with content.

#define INFILE @"foo"
#define OUTFILE @"bar"
#define KBufSize 512

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

   NSFileHandle *infile, *outfile;
   NSData *buffer;
   // NSStringEncoding *enc;
   infile = [NSFileHandle fileHandleForReadingAtPath: INFILE];
   if ( infile == nil ) {
      printf("Open %s for reading failed!\n", [INFILE UTF8String]);
      return 1;
   [[NSFileManager defaultManager] createFileAtPath:OUTFILE contents:nil attributes:nil];
   outfile = [NSFileHandle fileHandleForWritingAtPath: OUTFILE];
   if ( outfile == nil ) {
      printf("Open %s for writing failed!\n", [OUTFILE UTF8String]);
      return 2;
   // Truncate output file -- but is this necessary since it was created with nil content?
   // [outfile truncateFileAtOffset: 0];
   int t;
   for (t = 1, buffer = [infile readDataOfLength:KBufSize]; [buffer length] > 0; t++, buffer = [infile readDataOfLength:KBufSize]) {
      [outfile writeData:buffer];
      printf("Wrote %u bytes %i times\n", [buffer length], t);
   [infile closeFile];
   [outfile closeFile];
   // NSLog(@"%@", [NSString stringWithContentsOfFile:MYOUTFILE usedEncoding:enc error:NULL]);
   [pool drain];
    return 0;

This exercise asks to modify program 16.7.   Please note 2 things about the last NSLog call -- first, I commented it out because the input file I used was Foundation.h (file size larger than KBufSize).  The stringWithContentsOfFile: used in program 16.7 has been deprecated -- for NSString, it is stringWithContentsOfFile:usedEncoding:error:

I also commented out truncateFileAtOffset: as an experiment.

My question:  Is it necessary to invoke truncateFileAtOffset since the (output) file was created with nil content?


Hero Member
Posts: 3114

Reply #1 on: February 19, 2009, 11:52:54 AM

Yes, you're right.  The createFileAtPath:contents:attributes: method will zero out the file's contents, making the truncateFileAtOffset: method call unnecessary.


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.