2011年5月14日 星期六

Foundation Framework-檔案存取 CH16-8 NSFileHandle2


#import

#import

#import

#import

#import

#import


int main(int argc, char *argv[])

{

NSAutoreleasePool *pool=[[NSAutoreleasePool alloc]init];

NSFileHandle *inFile, *outFile;

NSData *buffer;

// Open the fileA for reading 開啟檔案A

inFile = [ NSFileHandle fileHandleForReadingAtPath: @"fileA"];

if(inFile == nil) {

NSLog(@"Open of fileA for reading failed");

return 1;

}

// Open the fileB for Update 開啟檔案B

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];// fileA內容寫到buffer

[outFile writeData:buffer];//buffer寫到fileB 的尾端

// Close the two files

[inFile closeFile];

[outFile closeFile];

[pool drain];

return 0;

}

沒有留言: