What is wrong with a standard truncate function ?

T

Timothy Madden

[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
 
V

vippstar

[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.
 
C

CBFalconer

Timothy said:
[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.
 
J

jameskuyper

CBFalconer said:
Timothy said:
[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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,983
Messages
2,570,187
Members
46,747
Latest member
jojoBizaroo

Latest Threads

Top