SetEndOfFile
The SetEndOfFile function establishes the specified end-of-file (EOF) position for a file.
BOOL SetEndOfFile(
HANDLE hFile
);
HANDLE hFile
);
Parameters
| Parameter | Description |
|---|---|
hFile |
A handle to the file. This handle must have been created by the CreateFile function and have GENERIC_WRITE access. |
Return value
If the function succeeds, the return value is nonzero. If the function fails, the return value is zero. 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 undefined.
If the file is opened with FILE_APPEND_DATA access, the end of the file is always positioned at the end of the file for writes. Calling SetEndOfFile when a file is opened with FILE_APPEND_DATA access can lead to unexpected behavior.
This function moves the current file pointer to the new end of the file.
Requirements
| Header | |
|---|---|
winbase.h |
|
Windows.h |
See also
Example
// This code example shows how to use SetEndOfFile. // It assumes hFile is a valid handle to a file opened with GENERIC_WRITE access. HANDLE hFile = ...; // Obtain a handle to the file LARGE_INTEGER newSize; newSize.QuadPart = 1024; // Set the new end-of-file position to 1024 bytes if (SetEndOfFile(hFile)) { // File successfully truncated or extended. // The file pointer is now at the new end-of-file. #ifdef _DEBUG OutputDebugString(L"SetEndOfFile succeeded.\n"); #endif } else { // Handle error DWORD error = GetLastError(); #ifdef _DEBUG WCHAR buffer[256]; wsprintf(buffer, L"SetEndOfFile failed with error code: %lu\n", error); OutputDebugString(buffer); #endif } // Remember to close the file handle when done // CloseHandle(hFile);