开发者

Text in file should not be overwritten

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

fileName = [[NSBundle mainBundle] pathForResource: @"user" ofType: @"txt"];
NSFileHandle *myHandle = [NSFileHandle fileHandleForUpdatingAtPath:fileNa开发者_如何学Gome];
[myHandle seekToEndOfFile];

NSData *dataName = [uname dataUsingEncoding: NSUnicodeStringEncoding];

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

[myHandle seekToEndOfFile];

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".

What is the problem with the code?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜