我有以下情况。我有一个大文件说 1 KB。我想读取前 100 个字节,然后从文件中删除 100 个字节读取数据,然后读取下一个 100 个字节。读取 100 个字节是可以的,但是如何从文件中删除 100 个字节?
最佳答案
这通常作为一个多步骤过程完成:
- 重命名原始文件。
- 用原来的文件名将你想要的数据写入一个新文件。
- 使用包含您不再需要的数据的临时名称删除旧文件。
这样,如果出现问题,您可以简单地恢复您重命名的原始文件。将文件从一个地方移动到另一个地方也是以这种方式实现的。
但是,如果您不想这样做,SetEndOfFile
function是另一种就地截断文件内容的可行选项。来自文档:
Sets the physical file size for the specified file to the current position of the file pointer.
The physical file size is also referred to as the end of the file. The
SetEndOfFile
function can be used to truncate or extend a file.
关于c++ - 如何在 Visual C++ 中截断文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6030072/