What is wrong with a standard truncate function ?

Discussion in 'C++' started by Timothy Madden, Nov 10, 2008.

  1. [cross-posted to comp.lang.c, comp.lang.c++]
    Hello

    I see there is now why to truncate a file (in C or C++)
    and that I have to use platform-specific functions for
    truncating files.

    Anyone knows why ? I mean C/C++ evolved over many years now,
    and still, people making _the_ standards never decided to
    include such a function. Even in POSIX it was included only
    in recent versions, mostly not fully supported yet.

    Why is that ? They must think there is something wrong with it,
    or that there are better ways to do it.

    I mean I want to write a portable application, I keep
    application data in a file, at some point the user
    deletes something from the data my program presents
    to him/her, and now I delete some data from the data file
    and want to shrink the file. Like a database system,
    after dropping some records or tables and compacting
    (or vacuum) the database. That is a good example
    of when a programmer legally needs to truncate a file.

    Copying only remaining data to a new file that will then
    replace the old one is not a solution for a large database,
    and leaving the empty space in the file is also not a
    solution for large files.

    Thank you,
    Timothy Madden
    Timothy Madden, Nov 10, 2008
    #1
    1. Advertising

  2. Timothy Madden

    Guest

    On Nov 10, 3:50 am, Timothy Madden <> wrote:
    > [cross-posted to comp.lang.c, comp.lang.c++]
    > Hello
    >
    > I see there is now why to truncate a file (in C or C++)
    > and that I have to use platform-specific functions for
    > truncating files.


    There are ways.

    int trunc(const char *file, size_t newsize) {
    FILE *fp;
    void *p = NULL;

    if(newsize) {
    fp = fopen(file, "rb");
    if(fp == NULL) return 1;
    p = malloc(newsize);
    if(p == NULL) { fclose(fp); return 1; } /* errno may be lost and
    the user will get a misleading error message */
    if(fread(p, 1, newsize, fp) != newsize) { fclose(fp); return
    1; } /* ditto */
    fclose(fp);
    }
    fp = fopen(file, "wb");
    if(fp == NULL) {
    free(p);
    return 1;
    }
    if(newsize) if(fwrite(p, 1, newsize, fp) != newsize) { free(p);
    fclose(fp); return 1; } /* ditto */
    free(p);
    return fclose(fp) == EOF;
    }



    }

    > Anyone knows why ? I mean C/C++ evolved over many years now,
    > and still, people making _the_ standards never decided to
    > include such a function. Even in POSIX it was included only
    > in recent versions, mostly not fully supported yet.
    >
    > Why is that ? They must think there is something wrong with it,
    > or that there are better ways to do it.


    You're right. There is something wrong with it, that some platforms
    that support C do not support file truncating (hell, some might not
    even support file operations at all). There are better ways, to use a
    more specific standard like POSIX.
    , Nov 10, 2008
    #2
    1. Advertising

  3. Timothy Madden

    CBFalconer Guest

    Timothy Madden wrote:
    >
    > [cross-posted to comp.lang.c, comp.lang.c++]
    >
    > I see there is now why to truncate a file (in C or C++)
    > and that I have to use platform-specific functions for
    > truncating files.


    That depends on file systems, so cannot be portable.

    Do not cross-post to c.l.c++. The languages are different.

    F'ups set.

    --
    [mail]: Chuck F (cbfalconer at maineline dot net)
    [page]: <http://cbfalconer.home.att.net>
    Try the download section.
    CBFalconer, Nov 10, 2008
    #3
  4. Timothy Madden

    jameskuyper Guest

    CBFalconer wrote:
    > Timothy Madden wrote:
    > >
    > > [cross-posted to comp.lang.c, comp.lang.c++]
    > >
    > > I see there is now why to truncate a file (in C or C++)
    > > and that I have to use platform-specific functions for
    > > truncating files.

    >
    > That depends on file systems, so cannot be portable.
    >
    > Do not cross-post to c.l.c++. The languages are different.


    Cross-posting reinstated.

    Don't overstate the differences between the languages. As far as this
    issue is concerned they are very similar. All of the <stdio.h>
    facilities are still present in C++, and the C++ standard mandates
    connections between the behavior of the <iostream> library and the
    behavior of the <stdio.h> library.

    Both committees are committed to, among other priorities, avoiding
    gratuitous incompatibilities between the two languages. Therefore, in
    the unlikely event that either committee were inclined to add a file
    truncation feature to their own language, the joint sub-committee
    would seriously consider recommending that it also be added to the
    other language in a compatible fashion.
    jameskuyper, Nov 10, 2008
    #4
    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. Yannick Turgeon

    Regexp to truncate

    Yannick Turgeon, Oct 11, 2003, in forum: Perl
    Replies:
    1
    Views:
    438
    Yannick Turgeon
    Oct 13, 2003
  2. Eddie
    Replies:
    1
    Views:
    9,684
    Arthur Yousif
    Dec 23, 2003
  3. Timothy Madden

    What is wrong with a standard truncate function ?

    Timothy Madden, Nov 10, 2008, in forum: C Programming
    Replies:
    3
    Views:
    1,979
    jameskuyper
    Nov 10, 2008
  4. omara007
    Replies:
    0
    Views:
    1,070
    omara007
    May 21, 2009
  5. Davy
    Replies:
    3
    Views:
    3,687
    jmeisell
    Jan 8, 2010
Loading...

Share This Page