使用目标c在文本文件中的指定行插入字符串 [英] Inserting a string at a specified line in text file using objective c

查看:104
本文介绍了使用目标c在文本文件中的指定行插入字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个包含以下数据的文本文件

Lets say I have a text file with the following data


用户名

Username

用户ID

详细信息

在用户ID和详细信息之间的新行用户屏幕名称。
我应该怎么做?
是否可以在特定行插入字符串?
因为这些对不同的用户有一个可变的字符串长度,我认为它不可能使用 seekToFileOffset 并更新文件。

suppose, I want to insert a new line "User screenname" inbetween "User ID" and "Details". 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 seekToFileOffsetand update the file. please help me out.

感谢

推荐答案

这样做的唯一方法是将文件读入内存,修改数据的内部表示,然后将其写回。

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.

编辑: 要澄清,你只能在最后增长一个文件;不在中间。

To clarify, you can only grow a file at the end; not in the middle.

这篇关于使用目标c在文本文件中的指定行插入字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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