在Windows上从文件中删除前N个字节的最有效的方法是什么? [英] What is the most efficient way to remove first N bytes from a file on Windows?

查看:373
本文介绍了在Windows上从文件中删除前N个字节的最有效的方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说,我有一个任意长度的文件 S ,我需要删除它的 N 字节( N 远远小于 S 。)在Windows上最有效的方法是什么? p>

我正在寻找一个WinAPI来做这个,如果有。



否则,我的选项是什么 - 将其加载到RAM中,然后使用剩余的数据重写现有文件? (在这种情况下,我不能确定PC有足够的RAM?)或将剩余的文件数据写入新文件,擦除旧文件,并将新文件重命名为旧文件。 (在这种情况下,如果这些步骤中的任何一个失败,该怎么办?)

解决方案

没有一般的方法来做这个内置的操作系统。有一些理论方法可以在扇区或集群边界上的操作系统下编辑文件系统的数据结构,但这对于每个文件系统都是不同的,并且需要违反任何安全模型。



要做到这一点,你可以读取从字节N开始的数据块,例如4k,然后从字节零开始将它们写回,然后使用文件truncate命令(setendoffile)在完成数据复制后设置文件的新的较小结尾。


Say, I have a file of an arbitrary length S and I need to remove first of its N bytes (where N is much less than S.) What is the most efficient way to do it on Windows?

I'm looking for a WinAPI to do this, if one is available.

Otherwise, what are my options -- to load it into RAM and then re-write the existing file with the remainder of data? (In this case I cannot be sure that the PC has enough RAM?) Or write the remainder of file data into a new file, erase the old one, and rename the new file into the old one. (In this case what to do if any of these steps fail? Plus how about defragmentation that this method causes on disk?)

解决方案

There is no general way to do this built into the OS. There are theoretical ways to edit the file system's data structures underneath the operating system on sector or cluster boundaries, but this is different for each file system, and would need to violate any security model.

To accomplish this you can read in the data starting at byte N in chunks of say 4k, and then write them back out starting at byte zero, and then use the file truncate command (setendoffile) to set the new smaller end of file when you are finished copying the data.

这篇关于在Windows上从文件中删除前N个字节的最有效的方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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