Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   ios::trunc vs unlink (http://www.velocityreviews.com/forums/t620421-ios-trunc-vs-unlink.html)

aryan 06-16-2008 10:40 AM

ios::trunc vs unlink
 
Hi,

I would like to know which of the following would be faster

unlink(filename);
fileName.open( filename,ios::out);

or

fileName.open( filename,ios::trunc | ios::out);



thanks.

Pascal J. Bourguignon 06-16-2008 11:55 AM

Re: ios::trunc vs unlink
 
aryan <mohan.kiran@gmail.com> writes:

> Hi,
>
> I would like to know which of the following would be faster
>
> unlink(filename);
> fileName.open( filename,ios::out);
>
> or
>
> fileName.open( filename,ios::trunc | ios::out);


Never mind the speed, they're not semantically equivalent.


Try it with filename="tata" on the file prepared as:

date > toto ; ln toto tata

The first one will keep the original file under the name toto, and
create a new file named tata, the second one will reset the file under
both names.


--
__Pascal Bourguignon__

aryan 06-16-2008 11:57 AM

Re: ios::trunc vs unlink
 
On Jun 16, 4:06*pm, Sam <s...@email-scan.com> wrote:
>
> Which one do you think would be faster?
>


I am not sure. If ios::trunc does not use unlink internally, it could
be the second form.



James Kanze 06-17-2008 07:15 AM

Re: ios::trunc vs unlink
 
On Jun 16, 1:55 pm, p...@informatimago.com (Pascal J. Bourguignon)
wrote:
> aryan <mohan.ki...@gmail.com> writes:


> > I would like to know which of the following would be faster


> > unlink(filename);
> > fileName.open( filename,ios::out);


> > or


> > fileName.open( filename,ios::trunc | ios::out);


> Never mind the speed, they're not semantically equivalent.


> Try it with filename="tata" on the file prepared as:


> date > toto ; ln toto tata


> The first one will keep the original file under the name toto,
> and create a new file named tata, the second one will reset
> the file under both names.


That particular example only applies to Unix. (And even under
Unix, who still uses hard links?) But your comment is
justified: deleting a file, then creating a new one, is not the
same thing as truncating an existing file. Under just about any
system, truncating a file will retain its old permissions;
deleting and recreating will establish new permissions. It
could have other impacts as well.

And while I'm at it, I might mention that the ios::trunc is a
no-op in the above. Anytime you open a file exclusively for
writing, it is truncated.

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34


All times are GMT. The time now is 06:12 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.