Réduire la table des matières
Développer la table des matières
Important Il est possible que le présent document ne corresponde pas aux pratiques recommandées pour le développement actuel. Par ailleurs, il se peut que des liens de téléchargement et d'autres ressources ne soient plus valides. La version recommandée actuelle est disponible ici. ArchiveDisclaimer


Changes the size of a file. A more secure version is available; see _chsize_s.

int _chsize( 
   int fd,
   long size 


File descriptor referring to an open file.


New length of the file in bytes.

_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, ENOSPC if no space is left on the device, or EINVAL if size is less than zero.

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.

This function validates its parameters. If size is less than zero or fd is a bad file descriptor, the invalid parameter handler is invoked, as described in Parameter Validation.


Required header

Optional header




For more compatibility information, see Compatibility in the Introduction.

// 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>
#include <share.h>

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

   // Open a file 
   if( _sopen_s( &fh, "data", _O_RDWR | _O_CREAT, _SH_DENYNO,
                 _S_IREAD | _S_IWRITE ) == 0 )
      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
© 2015 Microsoft