I am writing the following code to append fileA to fileB with no success.
Am I doing something wrong?
Thank you.
Code:
//Append the file "fileA" to the end of "fileB"
#import <Foundation/NSObject.h>
#import <Foundation/NSString.h>
#import <Foundation/NSFileHandle.h>
#import <Foundation/NSFileManager.h>
#import <Foundation/NSAutoreleasePool.h>
#import <Foundation/NSData.h>
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSFileHandle *inFile, *outFile;
NSData *buffer;
//Open the file fileA for reading
inFile = [NSFileHandle fileHandleForReadingAtPath:@"fileA"];
if (inFile == nil)
{
NSLog(@"Open of fileA for reading failed!");
return 1;
}
//Open the file fileB for updating
outFile = [NSFileHandle fileHandleForWritingAtPath:@"fileB"];
if (outFile = nil)
{
NSLog(@"Open of fileB for writing failed!");
return 2;
}
//Seek to the end of outfile
[outFile seekToEndOfFile];
//Read inFile and write its contents to outFile
buffer = [inFile readDataToEndOfFile];
[outFile writeData:buffer];
//Close the two files
[inFile closeFile];
[outFile closeFile];
//Verify its contents
NSLog(@"%@", [NSString stringWithContentsOfFile:@"fileB" encoding:NSUTF8StringEncoding error:NULL]);
[pool drain];
return 0;
}
Am I doing something wrong?
Thank you.