Syntax
#include <io.h> int _chsize(int handle, long size);Description
Value
This example opens a file named sample.dat and returns the current length of that file. It then alters the size of sample.dat and returns the new length of that file.
#include <io.h>#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
int main(void)
{
   long length;
   int fh;
   printf("\nCreating sample.dat.\n");
   system("echo Sample Program > sample.dat");
   if (-1 == (fh = open("sample.dat", O_RDWR|O_APPEND))) {
      printf("Unable to open sample.dat.\n");
      return EXIT_FAILURE;
   }
   if (-1 == (length = filelength(fh))) {
      printf("Unable to determine length of sample.dat.\n");
      return EXIT_FAILURE;
   }
   printf("Current length of sample.dat is %d.\n", length);
   printf("Changing the length of sample.dat to 20.\n");
   if (-1 == (_chsize(fh, 20))) {
      perror("chsize failed");
      return EXIT_FAILURE;
   }
   if (-1 == (length = _filelength(fh))) {
      printf("Unable to determine length of sample.dat.\n");
      return EXIT_FAILURE;
   }
   printf("New length of sample.dat is %d.\n", length);
   close(fh);
   return 0;
   /****************************************************************************
      The output should be similar to :
       Creating sample.dat.
       Current length of sample.dat is 17.
       Changing the length of sample.dat to 20.
       New length of sample.dat is 20.
   ****************************************************************************/
}
Related Information