ios::trunc vs unlink

Discussion in 'C++' started by aryan, Jun 16, 2008.

  1. aryan

    aryan Guest

    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.
     
    aryan, Jun 16, 2008
    #1
    1. Advertising

  2. aryan <> writes:

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


    --
    __Pascal Bourguignon__
     
    Pascal J. Bourguignon, Jun 16, 2008
    #2
    1. Advertising

  3. aryan

    aryan Guest

    On Jun 16, 4:06 pm, Sam <> 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.
     
    aryan, Jun 16, 2008
    #3
  4. aryan

    James Kanze Guest

    On Jun 16, 1:55 pm, (Pascal J. Bourguignon)
    wrote:
    > aryan <> writes:


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


    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:
    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
     
    James Kanze, Jun 17, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Paul Urbanus
    Replies:
    0
    Views:
    2,272
    Paul Urbanus
    Apr 7, 2006
  2. arti

    trunc in verilog

    arti, May 10, 2006, in forum: VHDL
    Replies:
    0
    Views:
    603
  3. Giulio

    undefined reference to `trunc'

    Giulio, Nov 24, 2004, in forum: C Programming
    Replies:
    7
    Views:
    3,401
    Dave Vandervies
    Nov 26, 2004
  4. =?Utf-8?B?RGF2ZQ==?=
    Replies:
    2
    Views:
    16,203
    Alexey Smirnov
    Jul 15, 2007
  5. Hicham Mouline
    Replies:
    2
    Views:
    830
    Keith Thompson
    Apr 23, 2010
Loading...

Share This Page