文件中的文本不应被覆盖 [英] Text in file should not be overwritten

查看:87
本文介绍了文件中的文本不应被覆盖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下代码写入文件。

I am using the following code to write in a file.

fileName = [[NSBundle mainBundle] pathForResource: @"user" ofType: @"txt"];
NSFileHandle *myHandle = [NSFileHandle fileHandleForUpdatingAtPath:fileName];
[myHandle seekToEndOfFile];

NSData *dataName = [uname dataUsingEncoding: NSUnicodeStringEncoding];

if ([dataName writeToFile:fileName atomically:YES])
    NSLog(@"writeok");

[myHandle seekToEndOfFile];

它会覆盖该文件。假设文件已经包含字符串box。现在当我写一个新词Hello,那么该文件只包含Hello,而不是框。

It overwrites the file. Suppose the file already contains the string "box". Now when I write a new word, "Hello", then the file contains only "Hello", not "box".

代码有什么问题? / p>

What is the problem with the code?

推荐答案

您无法写入包中的文件。由于Bundle是代码签名的,任何尝试
更改bundle中的文件都会破坏签名。您需要将文件移动到
Documents目录并在那里进行处理。

You cannot write to files in the Bundle. As the Bundle is code signed, any attempt to change the files in the bundle would break the signing. You need to move the file to the Documents directory and work on it there.

这篇关于文件中的文本不应被覆盖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆