SetEndOfFile function (Windows)

Switch View :
ScriptFree
SetEndOfFile function

Applies to: desktop apps | Metro style apps

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

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 client

Windows XP

Minimum supported server

Windows Server 2003

Header

FileAPI.h (include Windows.h);
WinBase.h on Windows Server 2008 R2, Windows 7, Windows Server 2008, Windows Vista, Windows Server 2003, and Windows XP (include Windows.h)

Library

Kernel32.lib

DLL

Kernel32.dll

See also

CloseHandle
CreateFile
CreateFileMapping
File Management Functions
SetFileInformationByHandle
SetFileValidData
UnmapViewOfFile

 

 

Send comments about this topic to Microsoft

Build date: 4/17/2012

Community Content

Neil.W
SetEndOfFile and OverLapped IO
If a file is opened with FILE_FLAG_OVERLAPPED, SetEndOfFile appears to truncate the file to zero bytes, regardless of the contents of OVERLAPPED->Offset.

msp66
Erratum: CreateFileMapping and SetEndOfFile

The documentation states: 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.

This is not the whole truth: It is possible to enlarge a file, while it is memory mapped. Only if you shrink it below the size of the memory mapping, then you will get the ERROR_USER_MAPPED_FILE error.

This observation has an important consequence: If a memory mapped file is shared between processes (with r/w access synchronized by some mutex), then one process can enlarge the file and map a bigger portion without having to wait for the other processes to close their views.

Indeed, Internet Explorer (resp. wininet.dll) enlarges its cache files using this technique. (To watch this, just set a breakpoint at wininet!MEMMAP_FILE::GrowMapFile and browse to some location with a lot of links and images, like the MSN homepage;-)


c45207
How to set file pointer

Something like SetFilePointerEx <http://msdn.microsoft.com/en-us/library/aa365542(v=VS.85).aspx> can be used to set the file pointer, which is then "committed" via SetEndOfFile.

"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. A write operation increases the size of the file to the file pointer position plus the size of the buffer written, leaving the intervening bytes uninitialized."