将文件清零
将全零写入文件的最有效、最快的方法是什么?包括错误检查。它只是 fwrite 吗?或者涉及 fseek 吗?
我在其他地方看过类似的代码:
off_t size = fseek(pFile,0,SEEK_END);
fseek(pFile,0,SEEK_SET);
while (size>sizeof zeros)
size -= fwrite(&address, 1, sizeof zeros, pFile);
while (size)
size -= fwrite(&address, 1, size, pFile);
其中零是我怀疑的文件大小的数组。不确定 off_t 到底是什么,因为它对我来说并不直接直观
What is the most efficient quickest way to write all zeros to a file? including error checking. Would it just be fwrite? or is fseek involved?
I've looked elsewhere and saw code similar to this:
off_t size = fseek(pFile,0,SEEK_END);
fseek(pFile,0,SEEK_SET);
while (size>sizeof zeros)
size -= fwrite(&address, 1, sizeof zeros, pFile);
while (size)
size -= fwrite(&address, 1, size, pFile);
where zeros is an array of file size I suspect. Not sure exactly what off_t was because it wasn't directly intuitive to me anyways
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您想要用相同长度的二进制零流替换文件的内容,还是简单地清空文件? (使其长度为零)
无论哪种方式,最好使用操作系统文件 I/O 原语来完成。选项一:
选项二:
错误处理留作练习。
Do you want to replace the contents of the file with a stream of binary zeroes of the same length, or do you want to simply empty the file? (make it have length zero)
Either way, this is best done with the OS file I/O primitives. Option one:
Option two:
Error handling left as an exercise.
mmap() 和 memset()
mmap() and memset()