将数组的内容写入文本文件 [英] Write contents of array to a text file
问题描述
我想将数组的内容写入我的iPhone应用程序中的文本文件。我可以从文本文件加载数组,但我想允许通过数组删除内容,并再次将内容写入文本文件。我怎样才能做到这一点?或者,如果有人可以指导我如何删除文本文件的最后一个单词也会有所帮助。
编辑: - 基本上我想删除文本中的最后一个单词文件。你可以帮助我实现这个目标。
I want to write the contents of an array to a text file in my iPhone application. I can load the array from the text file but i want to allow deleting of content through the array and again write the contents to the text file. How can I achieve this? Or simply if anyone can guide me how to delete the last word of a text file would also be helpful.
EDIT :- basically i want to delete the last word in a text file. can you help me with the logic to achieve that .
推荐答案
其他答案中的方法有效,文件是用XML编写的格式。简单地在文本文件中写入数组的内容,我首先将数组的所有字符串附加到一个大字符串然后将该字符串写入文本文件.below是我用于该代码的代码
the approach in other answers works bu the file is written in a XML format. to simply write the contents of the array in a text file , I first Appended all the strings of the array into a large string then write that string into the text file .below is the code i used for that
NSString *stringToWrite=[[NSString alloc]init];
for (int i=0; i<[stringArray count]; i++)
{
stringToWrite=[stringToWrite stringByAppendingString:[NSString stringWithFormat:@"%@ ",[stringArray objectAtIndex:i]]];
}
[stringToWrite writeToFile:textFilePath atomically:YES encoding:NSUTF8StringEncoding error:nil];
这将以纯文本格式写入数组的内容。
this will write the contents of the array in plain text format.
这篇关于将数组的内容写入文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!