Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You truncate a file by calling <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/aa365541.aspx" rel="nofollow"><code>SetFilePointer</code></a> or <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/aa365542.aspx" rel="nofollow"><code>SetFilePointerEx</code></a> to the desired location followed by <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/aa365531.aspx" rel="nofollow"><code>SetEndOfFile</code></a>. The following shows how a <code>truncate</code> function can be implemented:</p> <pre><code>bool truncate( HANDLE hFile, LARGE_INTEGER NewSize ) { LARGE_INTEGER Size = { 0 }; if ( GetFileSizeEx( hFile, &amp;Size ) ) { LARGE_INTEGER Distance = { 0 }; // Negative values move the pointer backward in the file Distance.QuadPart = NewSize.QuadPart - Size.QuadPart; return ( SetFilePointerEx( hFile, Distance, NULL, FILE_END ) &amp;&amp; SetEndOfFile( hFile ) ); } return false; } // Helper function taking a file name instead of a HANDLE bool truncate( const std::wstring&amp; PathName, LARGE_INTEGER NewSize ) { HANDLE hFile = CreateFileW( PathName.c_str(), GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL ); if ( hFile == INVALID_HANDLE_VALUE ) { return false; } bool Success = truncate( hFile, NewSize ); CloseHandle( hFile ); return Success; } </code></pre> <hr> <p><em>EDIT: Shorter Version</em> The <code>truncate</code> function can be shortened to the following:</p> <pre><code>bool truncate( HANDLE hFile, LARGE_INTEGER NewSize ) { return ( SetFilePointerEx( hFile, NewSize, NULL, FILE_BEGIN ) &amp;&amp; SetEndOfFile( hFile ) ); } </code></pre> <p>If you would rather want to pass the amount of bytes by which to shrink the file, the following implementation can be used:</p> <pre><code>bool truncate( HANDLE hFile, LARGE_INTEGER ShrinkBy ) { ShrinkBy.QuadPart = -ShrinkBy.QuadPart; return ( SetFilePointerEx( hFile, ShrinkBy, NULL, FILE_END ) &amp;&amp; SetEndOfFile( hFile ) ); } </code></pre> <hr> <p>To grow a file, open the file using <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/aa363858.aspx" rel="nofollow"><code>CreateFile</code></a> with a <code>dwDesiredAccess</code> that contains <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/aa364399.aspx" rel="nofollow"><code>FILE_APPEND_DATA</code></a>. Using <code>SetFilePointer</code> again to set the file pointer to the end of file you can then write new data calling <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/aa365747.aspx" rel="nofollow"><code>WriteFile</code></a>. For an example, see <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/aa363778.aspx" rel="nofollow">Appending One File to Another File</a>.</p> <hr> <p><em>EDIT: Growing a file without writing to it</em></p> <p>If you don't care about the file contents beyond the original file size you can apply the same sequence as shown for truncating a file to extend it:</p> <pre><code>bool SetFileSize( HANDLE hFile, LARGE_INTEGER NewSize ) { return ( SetFilePointerEx( hFile, NewSize, NULL, FILE_BEGIN ) &amp;&amp; SetEndOfFile( hFile ) ); } </code></pre> <p>This is documented behavior for <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/aa365531.aspx" rel="nofollow"><code>SetEndOfFile</code></a>:</p> <blockquote> <p>The <strong>SetEndOfFile</strong> 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.</p> </blockquote>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload