Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a file of arbitrary size using Windows C++ API

Tags:

c++

c

createfile

I would like to create a file of arbitrary size using the Windows C/C++ API. I am using Windows XP service pack 2 with a 32 bit virtual address memory space. I am familiar with CreateFile.

However CreateFile does not have a size arument, The reason I want to pass in a size argument is to allow me to create memory mapping files which allow the user to access data structures of predetermined size. Could you please advise of the proper Windows C/C++ API function which allow me to create a file of arbritrary predetermined size? Thank you

like image 747
Frank Avatar asked Dec 12 '22 16:12

Frank


2 Answers

You CreateFile as usual, SetFilePointerEx to the desired size and then call SetEndOfFile.

like image 102
Yakov Galka Avatar answered Jan 19 '23 00:01

Yakov Galka


To do this on UNIX, seek to (RequiredFileSize - 1) and then write a byte. The value of the byte can be anything, but zero is the obvious choice.

like image 33
brewbuck Avatar answered Jan 18 '23 23:01

brewbuck