chsize

From Appmethod Topics
Jump to: navigation, search

Go Up to io.h Index


Header File

io.h

Category

Input/output Routines

Prototype

int chsize(int handle, long size);

Description

Changes the file size.

chsize changes the size of the file associated with handle. It can truncate or extend the file, depending on the value of size compared to the file's original size.

The mode in which you open the file must allow writing.

If chsize extends the file, it will append null characters (\0). If it truncates the file, all data beyond the new end-of-file indicator is lost.

Return Value

On success, chsize returns 0. On failure, it returns -1 and the global variable errno is set to one of the following values:

EACCES

Permission denied

EBADF

Bad file number

ENOSPC

No space left on device



Example

#include <string.h>
#include <fcntl.h>
#include <io.h>
int main(void)
{
   int handle;
   char buf[11] = "0123456789";
   /* create text file containing 10 bytes */
   handle = open("DUMMY.FIL", O_CREAT);
   write(handle, buf, strlen(buf));
   /* truncate the file to 5 bytes in size */
   chsize(handle, 5);
   /* close the file */
   close(handle);
   return 0;
}

Portability

POSIX Win32 ANSI C ANSI C++

+