473,378 Members | 1,139 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,378 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 5293
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...

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.