Collapse the table of content
Expand the table of content
Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here. ArchiveDisclaimer


Changes the file size.

int _chsize( 
   int fd,
   long size 


File descriptor referring to open file.
New length of file in bytes.

Return Value

_chsize returns the value 0 if the file size is successfully changed. A return value of –1 indicates an error: errno is set to EACCES if the specified file is locked against access, to EBADF if the specified file is read-only or the descriptor is invalid, or to ENOSPC if no space is left on the device.

See _doserrno, errno, _sys_errlist, and _sys_nerr for more information on these, and other, return codes.


The _chsize function extends or truncates the file associated with fd to the length specified by size. The file must be open in a mode that permits writing. Null characters ('\0') are appended if the file is extended. If the file is truncated, all data from the end of the shortened file to the original length of the file is lost.


Routine Required header Optional headers Compatibility
_chsize <io.h> <errno.h> Win 98, Win Me, Win NT, Win 2000, Win XP

For additional compatibility information, see Compatibility in the Introduction.


All versions of the C run-time libraries.


// crt_chsize.c
/* This program uses _filelength to report the size
 * of a file before and after modifying it with _chsize.

#include <io.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>

int main( void )
   int fh, result;
   unsigned int nbytes = BUFSIZ;

   /* Open a file */
   if( (fh = _open( "data", _O_RDWR | _O_CREAT, _S_IREAD 
                   | _S_IWRITE ))  != -1 )
      printf( "File length before: %ld\n", _filelength( fh ) );
      if( ( result = _chsize( fh, 329678 ) ) == 0 )
         printf( "Size successfully changed\n" );
         printf( "Problem in changing the size\n" );
      printf( "File length after:  %ld\n", _filelength( fh ) );
      _close( fh );


File length before: 0
Size successfully changed
File length after:  329678

See Also

File Handling Routines | _close | _creat | _open | Run-Time Routines and .NET Framework Equivalents

© 2015 Microsoft