登录文本文件iPhone [英] Logging in a text file iPhone
问题描述
我有一个分数系统,我想将所有分数记录在一个由换行符分隔的文本文件中.这是我当前的保存代码:
I have a score system and I would like to log all scores in a text file separated by line breaks. Here is my current save code:
NSData *dataToWrite = [[NSString stringWithFormat:@"String to write ID:%i \n",random] dataUsingEncoding:NSUTF8StringEncoding];
NSString *docsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *path = [docsDirectory stringByAppendingPathComponent:@"text.txt"];
// Write the file
[dataToWrite writeToFile:path atomically:YES];
在检索此数据时,我只看到最新的保存.我该如何将其保存在列表中?
When retrieving this data, I only see the latest save. How do I make it so it saves all in a list?
谢谢.
推荐答案
[dataToWrite writeToFile:path atomically:YES];
覆盖该位置的文件,用dataToWrite
的内容替换那里的文件.
[dataToWrite writeToFile:path atomically:YES];
overwrites the file at that location, replacing whatever is there with the contents of dataToWrite
.
您可能会使用NSFileHandle
的fileHandleForWritingAtPath:
,然后调用seekToEndOfFile
附加到所述文件.
You can likely use NSFileHandle
's fileHandleForWritingAtPath:
and then call seekToEndOfFile
to append to said file.
你有例子吗?
Do you have an example?
尝试类似的东西:
NSFileHandle *f = [NSFileHandle fileHandleForWritingAtPath: p];
[f seekToEndOfFile];
[f writeData: d];
[f close];
全部键入SO;编译器/运行时可能与我对正确性的看法有所不同.
All typed into SO; the compiler/runtime might differ with my opinions of correctness.
这篇关于登录文本文件iPhone的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!