ios::trunc vs unlink

A

aryan

Hi,

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

unlink(filename);
fileName.open( filename,ios::eek:ut);

or

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



thanks.
 
P

Pascal J. Bourguignon

aryan said:
Hi,

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

unlink(filename);
fileName.open( filename,ios::eek:ut);

or

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

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.
 
J

James Kanze

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.
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top