470,815 Members | 1,103 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,815 developers. It's quick & easy.

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.
Jun 27 '08 #1
4 3957
Sam
aryan 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);
Which one do you think would be faster?

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)

iEYEABECAAYFAkhWSTsACgkQx9p3GYHlUOJ4wACffBpBKvYsUX Yk/xhrhbhWIauE
Kd4AnjW3A1YgleaqheZdJlDAjaF9xLgw
=uXY8
-----END PGP SIGNATURE-----

Jun 27 '08 #2
aryan <mo*********@gmail.comwrites:
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__
Jun 27 '08 #3
On Jun 16, 4:06*pm, Sam <s...@email-scan.comwrote:
>
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.
Jun 27 '08 #4
On Jun 16, 1:55 pm, p...@informatimago.com (Pascal J. Bourguignon)
wrote:
aryan <mohan.ki...@gmail.comwrites:
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:ja*********@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
Jun 27 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Marc Schellens | last post: by
8 posts views Thread by Brandon McCombs | last post: by
4 posts views Thread by Jon Hyland | last post: by
4 posts views Thread by Gactimus | last post: by
103 posts views Thread by Steven T. Hatton | last post: by
2 posts views Thread by Karl | last post: by
reply views Thread by Pixie Songbook | last post: by
9 posts views Thread by Ron Eggler | last post: by
reply views Thread by mihailmihai484 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.