#include <unistd.h> int ftruncate(int fn, off_t length);
ftruncate
truncates an OpenEdition file open for write
access to length
in bytes. fn
is the file descriptor for
the file to be truncated. ftruncate
does not change the current
file position.
ftruncate
returns a 0 if it is successful
and a - 1 if it is not successful.
length
is greater than the current file size, zero
bytes are added to the file at the end.
#include <unistd.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <stdio.h> main() { FILE *testfile; int fd, long size; char flname[] ="trunc.test"; if ((fd=creat(flname,S_IRUSR1)) < 0) perror("creat() error"); else { if ((testfile = fdopen(fd, "w")) == NULL) { perror("fdopen() error"); close(fd); } } fputs("ABCDEFGHIJKLMNOPQRSTUVWXYZ",testfile); fflush(testfile); size = lseek(fd, 0, SEEK_END); printf("The file is %ld bytes long.n", size); if (ftruncate(fd, 3) !=0) perror("ftruncate() error"); else { size = lseek(fd, 0, SEEK_END); printf("The file is now %ld bytes long.n", size); } fclose(testfile); unlink(flname); }
Copyright (c) 1998 SAS Institute Inc. Cary, NC, USA. All rights reserved.