C 文件流,附加在开头 [英] C file streams, appending at the beginning

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

问题描述

有没有一种简单的方法可以使用文件流在文本文件的开头附加一些内容?因为我能想到的唯一方法是将文件加载到缓冲区中,写入要追加的文本,然后写入缓冲区.我想知道是否可以在没有我的缓冲区的情况下进行.

Is there a simple way to append something at the beginning of a text file using file streams? Because the only way I can think of, is to load a file into a buffer, write text-to-append and then write the buffer. I want to know if it is possible to do it without my buffer.

推荐答案

不,这是不可能的.您必须重写文件以在开头插入文本.

No, its not possible. You'd have to rewrite the file to insert text at the beginning.

如果您使用临时文件,即可以避免将整个文件读入内存:

You could avoid reading the whole file into memory if you used a temporary file ie:

  1. 在文件开头写入你想要插入的值
  2. 从旧文件中读取 X 个字节
  3. 将这些 X 字节写入新文件
  4. 重复 2,3 直到读完旧文件
  5. 将新文件复制到旧文件中.

这篇关于C 文件流,附加在开头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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