文件更新问题 [英] File Updation problem

查看:68
本文介绍了文件更新问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用c更新文件.我希望通过插入的文本大小将文本移动适当的大小.使用fseek可以到达要更新的位置,但位置后的文本将被插入的文本替换.
例如
上一个

你叫什么名字?

更新后想要的结果(插入is)

您叫什么名字?

I want to update a File using c. I want the text to be shifted by the appropriate size by the size of the text inserted. Using fseek I can reach the place to be updated but the text after the position is being replaced by the inserted text.
e.g.
previous

What your name?

Wanted result after updation(insertion of is)

What is your name?

推荐答案

您不能.文本文件不支持插入.
最好的方法是创建一个临时文件,读取输入直到插入点,然后将其写入临时文件,然后在复制其余文本之前写待插入的文本.
然后,您可以删除原始文件,然后重命名临时文件.

还有其他方法,但这是最安全的.
You can''t. Text files do not support insert.
The best way is to create a temporary file, read the input up to the insertion point and write it to the temporary file, then write the text to be inserted before copying the remaining text.
You can then delete the original file, and rename the temporary.

There are other ways, but this is the safest.


您应该:
  1. 读取内存缓冲区中的文件内容.
  2. 更改文件内容(在缓冲区中).
  3. 将修改后的内容从内存缓冲区写回到内存中.原始文件.



我认为这是更有效的方法.



That''s the more effective way, in my opinion.


这篇关于文件更新问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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