Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > ios::trunc vs unlink

Reply
Thread Tools

ios::trunc vs unlink

 
 
aryan
Guest
Posts: n/a
 
      06-16-2008
Hi,

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

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

or

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



thanks.
 
Reply With Quote
 
 
 
 
Pascal J. Bourguignon
Guest
Posts: n/a
 
      06-16-2008
aryan <(E-Mail Removed)> writes:

> Hi,
>
> I would like to know which of the following would be faster
>
> unlink(filename);
> fileName.open( filename,ios:ut);
>
> or
>
> fileName.open( filename,ios::trunc | ios: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.


--
__Pascal Bourguignon__
 
Reply With Quote
 
 
 
 
aryan
Guest
Posts: n/a
 
      06-16-2008
On Jun 16, 4:06*pm, Sam <(E-Mail Removed)> 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.


 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      06-17-2008
On Jun 16, 1:55 pm, (E-Mail Removed) (Pascal J. Bourguignon)
wrote:
> aryan <(E-Mail Removed)> writes:


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


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


> > or


> > fileName.open( filename,ios::trunc | ios: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.


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:(E-Mail Removed)
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Making os.unlink() act like "rm -f" Roy Smith Python 2 12-11-2010 08:47 PM
Re: os.unlink on Windows Thomas Jollans Python 5 08-09-2010 08:08 AM
How can I unlink/delete an open file in Windows? Coffee Pot C Programming 12 10-19-2008 11:04 PM
perlcc error - 'Can't unlink error file...' Paul Urbanus Perl 0 04-07-2006 01:34 PM
os.unlink() AND win32api.DeleteFile() rbt Python 4 01-24-2006 01:17 PM



Advertisments