File Management Functions


SetEndOfFile Function

Sets the physical file size for the specified file to the current position of the file pointer.

The physical file size is also referred to as the end of the file. The SetEndOfFile function can be used to truncate or extend a file. To set the logical end of a file, use the SetFileValidData function.

Syntax

C++
BOOL WINAPI SetEndOfFile(
  __in  HANDLE hFile
);

Parameters

hFile [in]

A handle to the file to be extended or truncated.

The file handle must be created with the GENERIC_WRITE access right. For more information, see File Security and Access Rights.

Return Value

If the function succeeds, the return value is nonzero.

If the function fails, the return value is zero (0). To get extended error information, call GetLastError.

Remarks

The SetEndOfFile function can be used to truncate or extend a file. If the file is extended, the contents of the file between the old end of the file and the new end of the file are not defined.

Each file stream has the following:

  • File size: the size of the data in a file, to the byte.
  • Allocation size: the size of the space that is allocated for a file on a disk, which is always an even multiple of the cluster size.
  • Valid data length: the length of the data in a file that is actually written, to the byte. This value is always less than or equal to the file size.

The SetEndOfFile function sets the file size. Use SetFileValidData to set the valid data length.

If CreateFileMapping is called to create a file mapping object for hFile, UnmapViewOfFile must be called first to unmap all views and call CloseHandle to close the file mapping object before you can call SetEndOfFile.

Transacted Operations

If there is a transaction bound to the handle, then the change in the end-of-file position is transacted.

Requirements

Minimum supported clientWindows 2000 Professional
Minimum supported serverWindows 2000 Server
HeaderWinBase.h (include Windows.h)
LibraryKernel32.lib
DLLKernel32.dll

See Also

CloseHandle
CreateFile
CreateFileMapping
File Management Functions
SetFileInformationByHandle
SetFileValidData
UnmapViewOfFile

Send comments about this topic to Microsoft

Build date: 11/12/2009

Tags :


Page view tracker