Change the size of a file
#include <unistd.h>
int chsize( int filedes,
long size );
libc
The chsize() function extends or truncates the file specified by filedes to size bytes The file is padded with NUL ('\0') characters if it needs to be extended.
![]() |
chsize() ignores advisory locks that may have been set with the fcntl() function. |
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
int main( void )
{
int filedes;
filedes= open( "file", O_RDWR | O_CREAT,
S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP );
if( filedes!= -1 ) {
if( chsize( filedes, 32 * 1024L ) != 0 ) {
printf( "Error extending file\n" );
}
close( filedes);
return EXIT_SUCCESS;
}
return EXIT_FAILURE;
}
| Safety: | |
|---|---|
| Cancellation point | Yes |
| Interrupt handler | No |
| Signal handler | Yes |
| Thread | Yes |
close(), creat(), errno, ftruncate(), open()