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...
#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?
/* initialize buffer */
(buffer = [infile readDataOfLength: kBufSize]);
while (buffer.length != ZERO) {
[outfile writeData: buffer];
NSLog(@"Written: %i bytes", buffer.length);
(buffer = [infile readDataOfLength: kBufSize]);
};