Changes the file size.
#include <io.h> | Required only for function declarations | |
#include <errno.h> |
int _chsize( int handle, long size );
handle | Handle referring to open file | |
size | 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.
In DOS and Windows, the directory update is done when a file is closed. Consequently, while a program is running, requests to determine the amount of free disk space may receive inaccurate results.
The _chsize function returns the value 0 if the file size is successfully changed. A return value of –1 indicates an error, and errno is set to one of the following values:
Value | Meaning |
EACCES | Specified file is locked against access. |
EBADF | Specified file is read-only or an invalid file handle. |
ENOSPC | No space is left on device. |
Standards:UNIX
16-Bit:DOS, QWIN, WIN, WIN DLL
32-Bit:DOS32X
Use _chsize for compatibility with ANSI naming conventions of non-ANSI functions. Use chsize and link with OLDNAMES.LIB for UNIX compatibility.
/* 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>
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( _chsize( fh, 329678 ) == 0 )
printf( "Size successfully changed\n" );
else
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