简体   繁体   English

使用目标c在文本文件中的指定行插入字符串

[英]Inserting a string at a specified line in text file using objective c

Lets say I have a text file with the following data 可以说我有一个包含以下数据的文本文件

Username 用户名

User ID 用户身份

Details 细节

suppose, I want to insert a new line "User screenname" inbetween "User ID" and "Details". 假设我想在“用户ID”和“详细信息”之间插入新行“用户屏幕名称”。 How should I do this ? 我该怎么办? Is it possible to insert string at a specific line ? 是否可以在特定行插入字符串? since these have a variable string length for different users, i think its not possible to use seekToFileOffset and update the file. 由于这些文件针对不同的用户具有可变的字符串长度,因此我认为无法使用seekToFileOffset并更新文件。 please help me out. 请帮帮我。

Thanks 谢谢

The only way to do this is to read the file into memory, modify the internal representation of the data, and write it back out. 唯一的方法是将文件读入内存,修改数据的内部表示,然后将其写回。

There are some variations on this process, but in short; 此过程有一些变体,但总而言之; files are not like real paper and ink documents: You can't insert content into the middle of a file, only replace what is already there. 文件与真实的纸张和墨水文档不同:您不能在文件中间插入内容,只能替换已经存在的内容。

Edit: To clarify, you can only grow a file at the end; 编辑:为澄清起见,您只能在最后增加文件; not in the middle. 不在中间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM