This is the mail archive of the libc-help@sourceware.org mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: the file truncate API is limited and inconvenient


On Thu, May 06, 2010 at 08:43:39PM +0200, Krzysztof Åelechowski wrote:
>
>2. It is obvious for me that there should be a stdio API with the meaning "let 
>here be the end of this FILE".  I can do this under POSIX by using ftruncate 
>but it requires writing my own subroutine (ftuncate (fileno (), ftell())), and 
>of course there is no C++ way to do this.  How is it possible that nobody ever 
>needed this to be a standard API?

Well, afaik, file truncation is not supported on all platforms, at least in the
past. This is why there is no standard C file truncation function.

However, you can still do file truncation in a standard way, something like:

int standard_ftruncate(const char *path, size_t len)
{
    fp = fopen(path, "r");
    buf = malloc(len);
    fread(buf, 1, len, fp);
    fp = freopen(path, "w+", fp);
    fwrite(buf, 1, len, fp);
    free(buf);
    fclose(fp);
}

Definitely I omitted some return-value checking.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]