How to truncate filesize in C++

Discussion in 'C++' started by iceman, Jan 21, 2008.

  1. iceman

    iceman Guest

    Hi all,
    How do I truncate the file size.Is it possible to do this using
    fopen ,fseek and fwrite.
    Suppose I have a file of 30 KB if I want to reduce the size to
    15KB,Howcan I do this without overwriting the the file[ie opening the
    file for writing again and writing 15kb]

    Itried this but it didnt work.

    fseek(iofile,-150,SEEK_END);//150 is a random number
    fwrite(numbers, sizeof(char),0,iofile);

    Regards,
    Jegan
     
    iceman, Jan 21, 2008
    #1
    1. Advertising

  2. iceman wrote:
    > Hi all,
    > How do I truncate the file size.


    I don't think that C++ provides a standard way to do this, but I could
    be wrong.

    On POSIX systems, you need the ftruncate() or truncate() call. The
    former takes a file descriptor and the latter takes a filename. I'm not
    sure about other systems; you should ask on a newsgroup specific to your
    platform.


    Phil.
     
    Phil Endecott, Jan 21, 2008
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Arjen

    Format filesize in kB or MB

    Arjen, Aug 8, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    2,138
    Jerry III
    Aug 9, 2003
  2. David Colliver

    Problem retrieving filesize

    David Colliver, May 17, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    377
    David Colliver
    May 17, 2005
  3. =?Utf-8?B?ZG90bmV0dGVzdGVy?=

    Response.writefile (filename) ?? problem with filesize

    =?Utf-8?B?ZG90bmV0dGVzdGVy?=, Aug 16, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    503
    Buddy Ackerman
    Aug 16, 2005
  4. VB Programmer

    How check filesize using FileUpload control?

    VB Programmer, Jan 18, 2006, in forum: ASP .Net
    Replies:
    11
    Views:
    20,396
  5. Michel Rosien

    STL Stream maximum filesize?

    Michel Rosien, Sep 18, 2003, in forum: C++
    Replies:
    8
    Views:
    619
    Mike Wahler
    Sep 18, 2003
Loading...

Share This Page