2011年5月9日 星期一

Foundation Framework-檔案存取 CH16-2 NSData

//

// main.m

// CH16-2 NSData

//

// Created by jason on 2011/5/9.

// Copyright 2011 __MyCompanyName__. All rights reserved.

//

NSData 可以建立緩衝區(Buffer) 可以用來讀取檔案,或是將buffer寫入檔案

32 bit的程式 buffer大小可以為2GB

64 bit的程式 buffer大小可以為8EB(exabytes)或是8000PB

P.S. 1Mega = 1024*1024

1Giga = 1024*1024*1024

1Tera = 1024*1024*1024*1024

1Peta = 1024*1024*1024*1024*1024

1Exa = 1024*1024*1024*1024*1024*1024

===========================================================


#import

#import

#import

#import

#import


//#import


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

{

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

NSFileManager *fm;

NSData *fileData;//建立buffer

fm=[NSFileManager defaultManager];

//Read the file newfile2

fileData = [fm contentsAtPath:@"newfile1"];// "contentsAtPath" 將指定檔案讀取到已建立的Buffer

if(fileData==nil){

NSLog(@"File read failed!");

return 1;

}

//Write the data to newfile3

//Buffer(fileData)內的資料 寫入newfile3

//如果沒有這個檔案它會新增,如果有會覆蓋

if([fm createFileAtPath:@"newfile3" contents:fileData attributes:nil]==NO){

NSLog(@"Could not create the copy!");

return 2;

}

NSLog(@"File copy was successful!");

[pool drain];

return 0;

}


沒有留言: