将变量和字符串写入文本文件(objective-c) [英] Writing variables and strings to a text file (objective-c)

查看:142
本文介绍了将变量和字符串写入文本文件(objective-c)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常在网上冲浪,但是仍然找不到我的问题的答案:如何将变量写入.txt文件以及将NSStrings写入.txt文件的最佳方法是什么.我尝试使用 writeToFile 方法,但是如果我有两次调用此方法,则第一个调用时写入的文本将被覆盖.我想了解发生的事情的历史,以及.txt文件中某些变量的值.我该怎么办?

I was surfing on the Net a lot, but still I haven't found an answer to my question: how to write variables to a .txt file and what's the best way to write NSStrings to .txt files. I've tried using writeToFile method, but if I have two calls of this method, then the text that was written when the first one was called will be overwritten. I want to have a kind of history of what has happened, and values of some variables in my .txt file. How should I do it?

推荐答案

如果只想将字符串附加到.txt文件,则可以执行以下操作:

If you just want to append a string to a .txt file, you could do this:

NSString *string = ...;   // your string
NSString *path = ...;     // path to your .txt file
// Open output file in append mode:
NSOutputStream *stream = [[NSOutputStream alloc] initToFileAtPath:path append:YES];
[stream open];
// Make NSData object from string:
NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
// Write data to output file:
[stream write:data.bytes maxLength:data.length];
[stream close];

这篇关于将变量和字符串写入文本文件(objective-c)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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