2012年5月26日 星期六

File Handle Operations

refer to  Addison Wesley - Programming.in.ObjectiveC.2.0.2nd (2009)

#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, 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 outfile 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
buffer = [inFile readDataToEndOfFile]; //把檔案中所有資料讀出
[outFile writeData: buffer]; //寫入輸出檔

// Close the two files
[inFile closeFile];
[outFile closeFile];

// Verify the file ’ s contents
NSLog(@ ” %@ ” , [NSString StringWithContentOfFile: @ ” testout ” ]);
[pool drain];
return 0;
}

沒有留言: