c++ - 在 C/C++ 中创建用零填充的 1GB 文件的最快方法是什么?

标签 c++ windows file-io

我需要在 Windows 操作系统中创建一个充满零的巨大二进制文件。它的大小是给定的。在 C/C++ 中最有效的方法是什么?

最佳答案

尝试调用truncate(2):

truncate("/my/file", my_size);

更多信息,

man 2 truncate

The truncate() and ftruncate() functions cause the regular file named by path or referenced by fd to be truncated to a size of precisely length bytes.

If the file previously was larger than this size, the extra data is lost. If the file previously was shorter, it is extended, and the extended part reads as null bytes ('\0').

如果您正在寻找 Windows 原生功能,请查看 SetEndOfFile ,它的作用大致相同。

关于c++ - 在 C/C++ 中创建用零填充的 1GB 文件的最快方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9809512/

相关文章:

java - 如何在Java中使用相对路径编写文件

c++ - 计算二维惯性张量

windows - GDI+后台线程有什么作用?

mongodb - 将文件存储在磁盘或 MongoDB 上

c - 在 C 中从较长字符串中提取子字符串的最佳方法是什么

python - tkinter.TclError : image "pyimage3" doesn't exist 错误

c++ - 我是否需要遍历两个子树进行计算,从根到叶的总和,到给定值?

c++ - C仅使用指针从多维数组中提取特定列

c++ - C和C++中函数赋值给变量的区别

windows - VSCode : How to debug current Jest test file