the file truncate API is limited and inconvenient
Fri May 7 09:12:00 GMT 2010
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);
Definitely I omitted some return-value checking.
More information about the Libc-help