user1343318
user1343318

Reputation: 2141

Is there a function in Windows API similar to ftruncate on POSIX?

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

Answers (3)

Mark Adler
Mark Adler

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

Jim Rhodes
Jim Rhodes

Reputation: 5095

Use SetFilePointer or SetFilePointerExto set the current position to the size you want, then call SetEndOfFile.

Upvotes: 6

Zac Howland
Zac Howland

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

Related Questions