Changes the file size.
int _chsize( int handle, long size );
Routine Required Header Optional Headers Compatibility
_chsize <io.h> <errno.h> Win 95, Win NT
For additional compatibility information, see Compatibility in the Introduction.
LIBC.LIB Single thread static library, retail version
LIBCMT.LIB Multithread static library, retail version
MSVCRT.LIB Import library for MSVCRT.DLL, retail version
_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 handle is invalid, or to ENOSPC if no space is left on the device.
Handle referring to open file
New length of file in bytes
The _chsize function extends or truncates the file associated with handle 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.
/* CHSIZE.C: This program uses _filelength to report the size
* of a file before and after modifying it with _chsize.
void 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
File Handling Routines
See Also _close, _creat, _open