CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Apr 2001
    Posts
    189

    truncating a file

    How do I truncate a file's size using ANSI C?

    The file's size is 200 bytes, I want to shrink it to 100 bytes. I know how to do this in POSIX and Win32, but is there no standard ANSI C method for accomplishing this simple task?

    TekBoy

    P.S. please always rate all post that help you. I will do the same....

  2. #2

    Re: truncating a file

    Hi,
    You can use chsize(),

    #include <fcntl.h>
    #include <io.h>


    int main(void)
    {
    int handle;

    handle = open("C:\\a.txt", O_RDWR);


    /* truncate the file to 50 bytes in size */
    chsize(handle, 50);

    /* close the file */
    close(handle);
    return 0;
    }






    Regards,
    Sreedharan.
    Regards,
    Sreedharan

  3. #3
    Join Date
    Aug 2000
    Location
    New Jersey
    Posts
    968

    Re: truncating a file

    chsize() is not part of the C/C++ standards.

    You can try using fseek and fflush to extend the file length.

    http://www.axter.com
    David Maisonave
    Author of Policy Based Synchronized Smart Pointer
    http://axter.com/smartptr


    Top ten member of C++ Expert Exchange.
    C++ Topic Area

  4. #4
    Join Date
    Apr 2001
    Posts
    189

    Re: truncating a file

    Thanks for your help, but unfortunately chsize() is not ANSI C :-(

    TekBoy

    P.S. please always rate all post that help you. I will do the same....

  5. #5
    Join Date
    Apr 2001
    Posts
    189

    Re: truncating a file

    Thanks, but I would like to truncate (shrink) the file, not extend it.

    TekBoy

    P.S. please always rate all post that help you. I will do the same....

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured