在Windows上创建的文件大 [英] Creating big file on Windows
问题描述
我需要创建大较大(1-8 GB)文件。什么是在Windows上这样做的最快方式使用C或C ++?我需要动态地创建他们的速度实在是一个问题。文件将被用于存储仿真即会随机获得不同的偏移,我需要所有的存储将是preallocate但没有初始化,目前我们正在编写与虚拟数据的所有存储和它的时间太长。
I need to create big relatively big (1-8 GB) files. What is the fastest way to do so on Windows using C or C++ ? I need to create them on the fly and the speed is really an issue. File will be used for storage emulation i.e will be access randomly in different offsets and i need that all storage will be preallocate but not initialized, currently we are writing all storage with dummy data and it's taking too long.
感谢。
推荐答案
使用Win32 API中,的的CreateFile , SetFilePointerEx ,的SetEndOfFile 和的 CloseHandle的。在该相同的顺序。
Use the Win32 API, CreateFile, SetFilePointerEx, SetEndOfFile, and CloseHandle. In that same order.
诀窍是在SetFilePointerEx功能。从MSDN:
The trick is in the SetFilePointerEx function. From MSDN:
请注意,这是不设置误差
文件指针超越的位置
该文件的结束。的大小
文件不增加,直到你打电话
在SetEndOfFile,WriteFile的,或
WriteFileEx功能。
Note that it is not an error to set the file pointer to a position beyond the end of the file. The size of the file does not increase until you call the SetEndOfFile, WriteFile, or WriteFileEx function.
Windows资源管理器复制从一个地方到另一个文件时实际上做同样的事情。它这样做是让磁盘不需要重新分配的文件零散的磁盘。
Windows explorer actually does this same thing when copying a file from one location to another. It does this so that the disk does not need to re-allocate the file for a fragmented disk.
这篇关于在Windows上创建的文件大的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!