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

查看:113
本文介绍了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.

编辑:如果使用临时文件,则可以避免将整个文件读入内存,即: / p>

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. 将新文件复制到旧文件中。

  1. Write the value you want inserted at the beginning of the file
  2. Read X bytes from the old file
  3. Write those X bytes to the new file
  4. Repeat 2,3 until you are done reading the old file
  5. Copy the new file to the old file.

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

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