Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating big file on Windows

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.

Thanks.

like image 754
Ilya Avatar asked Jan 18 '09 15:01

Ilya


People also ask

How do I create a 10gb dummy?

There are two commands you can enter in the Command Prompt to create a dummy file: fsutil file createnew filename size. fsutil file createnew pathfilename size.

How do I make multiple text files in Windows?

If all you need is a simple one-line text file, you can use the echo command, whereas if you want to create a text file with multiple lines of text, you can either use the copy con command or create the file in Notepad. Lastly, if you want to create multiple text files, you can use the for loop.


1 Answers

Use the Win32 API, CreateFile, SetFilePointerEx, SetEndOfFile, and CloseHandle. In that same order.

The trick is in the SetFilePointerEx function. From MSDN:

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 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.

like image 130
Brian R. Bondy Avatar answered Nov 02 '22 07:11

Brian R. Bondy