Retro video games delivered to your door every month!
Click above to get retro games delivered to your door ever month!
X-Hacker.org- Borland C++ 2.x ( with Turbo C ) - <b>chsize() changes file size</b> http://www.X-Hacker.org [<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
 chsize()                Changes File Size

 #include <io.h>

 int        chsize(handle,size);
 int        handle;
 long       size;

    chsize() changes the size of the file associated with 'handle'. In
    order for the change to take place, the file must have been opened
    with write permission.  The file can then be made larger or smaller,
    depending on the value of 'size' in relation to the original size of
    the file. If chsize() makes the file larger, it is padded with null
    characters ('\0').  If it is made smaller, it is truncated and all
    data past the new end-of-file indicator is lost.

       Returns:     0 on success. -1 is returned on failure, and 'errno'
                    is set to either

                    EACCES         Permission denied, or
                    EBADF          Bad file number.

   -------------------------------- Example ---------------------------------

    The following statements create a file with write-permission then
    extend its size to 1024 bytes.

           #include <stdio.h>      /* for printf */
           #include <sys\stat.h>
           #include <io.h>         /* for creatnew */

           main()
           {
           int handle;
           int result;
           long size = 1024L;

               if ((handle = creat("newfile.dat",S_IREAD|S_IWRITE)) == -1)
                   printf("couldn't create NEWFILE\n");
               else {
                   printf("NEWFILE created\n");
                   if ((result = chsize(handle,size)) != 0)
                       printf("unable to change file size\n");
                   else
                       printf("file size changed to %ld\n",size);
               close(handle);
               }
           }


See Also: creat() fopen()

Online resources provided by: http://www.X-Hacker.org --- NG 2 HTML conversion by Dave Pearson