2012-03-19 45 views
2

現有的文件我開發的Mac Cocoa應用程序。我必須將文件的數據附加到新行中的現有文件中。我想通過下面的代碼來做到這一點:將數據添加到新線可可

NSData * theData = [NSData dataWithContentsOfFile: @"~/Desktop/test/new.rtf" 
              options: NSMappedRead 
              error: &error]; 
NSFileHandle *output = [NSFileHandle fileHandleForWritingAtPath:@"~/Desktop/test/test.rtf"]; 
[output seekToEndOfFile]; 
[output writeData:theData]; 

但這種代碼是行不通的。這段代碼什麼都不做。既不提供任何錯誤,也不將文件new.rtf的數據寫入test.rtf。任何想法如何將文件new.rtf的數據追加到test.rtf中的新行?

回答

3
NSString *readFile = [@"~/Desktop/test/new.rtf" stringByExpandingTildeInPath]; 
NSString *writeFile = [@"~/Desktop/test/test.rtf" stringByExpandingTildeInPath]; 

NSData * theData = [NSData dataWithContentsOfFile:readFile 
              options:NSMappedRead 
              error:NULL]; 

NSFileHandle *output = [NSFileHandle fileHandleForUpdatingAtPath:writeFile]; 
[output seekToEndOfFile]; 
[output writeData:theData]; 
[output closeFile]; 
+0

我已經被充分路徑,但它不工作嘗試。 – 2012-03-20 07:48:13

+0

如果關閉文件句柄,使用'[output closeFile];'?怎麼辦? – Enchilada 2012-03-21 00:14:28

+0

即使關閉文件句柄後也沒有發生。 – 2012-03-21 11:27:34

相關問題