#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;
}