cocoa - 将数据附加到新行 cocoa 中的现有文件

标签 cocoa file

我正在为 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 中吗?

最佳答案

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];

关于cocoa - 将数据附加到新行 cocoa 中的现有文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9768256/

相关文章:

objective-c - 更改 NSOpenPanel 按钮文本

xcode - 每个 Nib 使用 1 个 Controller 转换新 xcode cocoa 项目的步骤

iphone - Mac 和 iPhone 上的共享模型

linux - 通过在 bash 中附加来有条件地合并文件

c++ - 处理记录最后 1000 个事件的文件操作的最佳方法

iphone - self.myIvar 和 myIvar 之间有什么不同?

swift - 如何从多个选项卡 View 创建到共享用户默认值 Controller 的绑定(bind)

linux - 从具有多字符分隔符的文本文件中提取列,即 "%$%"

c++ - 如何在没有getline的情况下在c++中获取文件中的空格字符

ios - 从 PC 上的 iPhone App 的文档文件夹下载创建的文件