473,394 Members | 1,852 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

What is wrong with a standard truncate function ?

[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
Nov 10 '08 #1
4 5296
On Mon, 10 Nov 2008 03:50:34 +0200, Timothy Madden
<te**********@gmail.comwrote in comp.lang.c:
[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.
If your platform has such functions.
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.
You seem to think that people making _the_ standards get to impose
their will by fiat.
Why is that ? They must think there is something wrong with it,
or that there are better ways to do it.
Or perhaps they think it is not generally useful enough.
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.
No, that is an example of when a programmer might want 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.
In your opinion, rewriting a file is "not a solution". I am sure that
others disagree. In fact, I think that there are many who would
disagree with compacting an existing file. Much too risky.

So why is it "not a solution"? For "large files", whatever your
definition of "large" is, I would suspect most programmers these days
would use a database engine. They are available for all popular
platforms, and even good, free open source versions are available. You
could use the database library to take care of details like this.

The simple fact is that it is not the language standard's job to add
every single function that some programmers might find useful. Once a
function is added to the standard library, every conforming
implementation needs to implement. Regardless of whether or not the
underlying operating system makes it easy.

There are many new language features or library functions that some
particular programmer thinks would make his/her job much easier, so
much so that it is worth the effort of every single compiler
implementer to add it to their compiler or library.

But no matter how useful such a function might be to you, there is a
cost/benefit calculation that need to be made.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
Nov 10 '08 #2
On Nov 10, 3:50 am, Timothy Madden <terminato...@gmail.comwrote:
[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 '08 #3
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.
Nov 10 '08 #4
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 <iostreamlibrary and the
behavior of the <stdio.hlibrary.

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.
Nov 11 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: elyob | last post by:
How can I get -0.162058 to -0.1 rather than rounding to -0.2? I know I'm being stupid here, but it's really bugging me. The number before the decimal point could 0 - 999 Thanks
2
by: Ed | last post by:
I am trying to get some information to compare and contrast the Truncate Table function and the Drop Table function. I know that using Truncate Table is faster and saves the structure of the table...
3
by: martin | last post by:
Hi, We have a heavily used production server and a table which logs every hit on a web site. This table has grown large over time and we want to clear it down as efficiently as possible. We would...
72
by: E. Robert Tisdale | last post by:
What makes a good C/C++ programmer? Would you be surprised if I told you that it has almost nothing to do with your knowledge of C or C++? There isn't much difference in productivity, for...
15
by: XZ | last post by:
Hi everyone, this is really confusing to me: #include <stdio.h> main(int argc, char **argv) { printf("argv = %f\n",(double)atof(argv)); printf("argv = %d\n\n",atoi(argv)); } $ a.out a argv...
5
by: J-P-W | last post by:
I have some code, from this group (many thanks) that sends an attachment to an email. The following: Dim objNewMail As Outlook.MailItem Dim golApp As...
10
by: silverburgh.meryl | last post by:
Hi, Can you please tell me what is the mean of 't' in the input parametre in utf8_fopen? utf8_fopen( a_file, "wt" ) Thank you.
3
by: Kurt | last post by:
Hi, I'd like advices about an idea I add to resolve a problem. thanks to you in advance for yours answers. I have a database with tables that I load with flat file. The size of each table is...
5
by: Timothy Madden | last post by:
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...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.