Reputation: 2141
I am using CreateFile function from Windows API to open a file. Now this function returns a HANDLE
object on which we further use Read or Write or any of the filing operations.
I have found SetEndOfFile which talks about truncating files but it has no parameter for size of file. Finally, _chsize function doesn't accept a HANDLE
object. Is there a way around this?
Upvotes: 2
Views: 5630
Reputation: 112502
Yes! _chsize_s()
in io.h. Works just like ftruncate()
, with the same parameters and all. Just #define ftruncate _chsize_s
. (Use _chsize_s()
with a 64-bit offset parameter, as opposed to _chsize()
which has a 32-bit offset parameter.)
Upvotes: 1
Reputation: 5095
Use SetFilePointer
or SetFilePointerEx
to set the current position to the size you want, then call SetEndOfFile
.
Upvotes: 6
Reputation: 15870
SetEndOfFile
will use the current file pointer location within the file to truncate it at that point. If you want a specific size, you can use SetFilePointer
to move the pointer to the desired location.
Upvotes: 0