在iphone上逐行写数据 [英] write data line by line on iphone

查看:104
本文介绍了在iphone上逐行写数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以在iphone上写一个文本文件..但是每次我写的我的上一个值都被删除了,有没有办法继续写数据分开\ n ??

i can write to a text file on iphone..but each time i write my previous value is erased, is there any way to keep writing data separated by \n??

这是我的代码

NSString *cc=@"1";

    [cc writeToFile:storePath atomically:YES];

    NSString *myText1 = [NSString stringWithContentsOfFile:storePath];

    NSLog(@"text is %@",myText1);

这给了我1现在我想添加2个像1然后2个

this gives me 1 now i want to add 2 like 1 and then 2

推荐答案

使用 NSFileHandle 方法 seekToEndOfFile 和调用 writeData

NSFileHandle *aFileHandle;
NSString *aFile;

aFile = [NSString stringWithString:@"Your File Path"]; //setting the file to write to

aFileHandle = [NSFileHandle fileHandleForWritingAtPath:aFile]; //telling aFilehandle what file write to
[aFileHandle truncateFileAtOffset:[aFileHandle seekToEndOfFile]]; //setting aFileHandle to write at the end of the file

[aFileHandle writeData:[toBeWritten dataUsingEncoding:nil]]; //actually write the data

这篇关于在iphone上逐行写数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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