open for writing without immediately erasing

Discussion in 'Perl Misc' started by mike, Mar 3, 2009.

  1. mike

    mike Guest

    is there a way to do 'open(FH, ">", x)' without erasing what's in the
    file? I want to open a file for writing then when I actually do the
    write, keep replacing just the first line (all that's ever in the file is
    a single line at the top) - but I don't want the (original) first line
    erased until I do my first new write.

    I tried ">>" and iterations of "seek(FH, -20, 0);" fiddling with
    position/whence but it seems (to me) that with ">>" perl treats the
    'beginning' of the file as whatever position followed what existed in the
    file when it was opened; iow I can't go to absolute 0 in the file (or
    rather am having trouble figuring out how to go to 0).

    thanks.

    --
     
    mike, Mar 3, 2009
    #1
    1. Advertising

  2. Nathan Keel <> wrote:
    > Open with +< or +> to open for read and write (for overwrite, but not
    > append). +> will open and create (if the file doesn't exist) and
    > otherwise will truncate (which you seem to not want), so try +<. Open
    > the file, read in the current value and write the new value. Yes, you
    > should also read up on seek() as well as use the now preferred 3
    > argument open.


    And then you'll probably want to truncate() as well. Otherwise if you
    write a short line while a longer line was present, it'll leave remnants
    past the end of the recent write.

    --
    Darren
     
    Darren Dunham, Mar 3, 2009
    #2
    1. Advertising

  3. mike

    Nathan Keel Guest

    mike wrote:

    >
    > is there a way to do 'open(FH, ">", x)' without erasing what's in the
    > file? I want to open a file for writing then when I actually do the
    > write, keep replacing just the first line (all that's ever in the file
    > is a single line at the top) - but I don't want the (original) first
    > line erased until I do my first new write.
    >
    > I tried ">>" and iterations of "seek(FH, -20, 0);" fiddling with
    > position/whence but it seems (to me) that with ">>" perl treats the
    > 'beginning' of the file as whatever position followed what existed in
    > the file when it was opened; iow I can't go to absolute 0 in the file
    > (or rather am having trouble figuring out how to go to 0).
    >
    > thanks.
    >
    > --


    Open with +< or +> to open for read and write (for overwrite, but not
    append). +> will open and create (if the file doesn't exist) and
    otherwise will truncate (which you seem to not want), so try +<. Open
    the file, read in the current value and write the new value. Yes, you
    should also read up on seek() as well as use the now preferred 3
    argument open.
     
    Nathan Keel, Mar 3, 2009
    #3
  4. mike

    Uri Guttman Guest

    >>>>> "CM" == Chris Mattern <> writes:

    CM> On 2009-03-03, mike <> wrote:
    >>
    >> is there a way to do 'open(FH, ">", x)' without erasing what's in the
    >> file? I want to open a file for writing then when I actually do the
    >> write, keep replacing just the first line (all that's ever in the file is
    >> a single line at the top) - but I don't want the (original) first line
    >> erased until I do my first new write.
    >>
    >> I tried ">>" and iterations of "seek(FH, -20, 0);" fiddling with
    >> position/whence but it seems (to me) that with ">>" perl treats the
    >> 'beginning' of the file as whatever position followed what existed in the
    >> file when it was opened; iow I can't go to absolute 0 in the file (or
    >> rather am having trouble figuring out how to go to 0).
    >>

    CM> I recommend Tie::File.

    the OP isn't clear about what he really wants.

    >> keep replacing just the first line (all that's ever in the file is
    >> a single line at the top)


    if all he really has is a single line then why is he worrying about
    overwriting it? and saying he only has a single line at the top of the
    file makes little sense as a single line is ALWAYS at the top (and the
    bottom) of the file. if he always wants the file in a known state then
    Tie::File may not be good enough unless it locks it and all readers obey
    the lock (not likely).

    File::Slurp's write_file with the atomic option would do the trick. it
    writes to a temp file and does a rename to the original file name which
    is an atomic operation. then the file is always in a known and clean
    state. but the OP still needs to clear up the muddy requirements IMO.

    uri

    --
    Uri Guttman ------ -------- http://www.sysarch.com --
    ----- Perl Code Review , Architecture, Development, Training, Support ------
    --------- Free Perl Training --- http://perlhunter.com/college.html ---------
    --------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
     
    Uri Guttman, Mar 3, 2009
    #4
  5. mike

    Dr.Ruud Guest

    mike wrote:

    > is there a way to do 'open(FH, ">", x)' without erasing what's in the
    > file? I want to open a file for writing then when I actually do the
    > write, keep replacing just the first line (all that's ever in the file is
    > a single line at the top) - but I don't want the (original) first line
    > erased until I do my first new write.
    >
    > I tried ">>" and iterations of "seek(FH, -20, 0);" fiddling with
    > position/whence but it seems (to me) that with ">>" perl treats the
    > 'beginning' of the file as whatever position followed what existed in the
    > file when it was opened; iow I can't go to absolute 0 in the file (or
    > rather am having trouble figuring out how to go to 0).


    Just don't open it until you need to write to it?
    (or is the openness meant as a lock?)

    --
    Ruud
     
    Dr.Ruud, Mar 3, 2009
    #5
    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. Rajorshi  Biswas
    Replies:
    3
    Views:
    388
    Rajorshi Biswas
    Jul 20, 2006
  2. Lave
    Replies:
    0
    Views:
    262
  3. Lie Ryan
    Replies:
    6
    Views:
    248
    Lie Ryan
    Oct 26, 2008
  4. Janus Bor
    Replies:
    6
    Views:
    121
    Dave Bass
    Jul 16, 2008
  5. Eric
    Replies:
    4
    Views:
    156
Loading...

Share This Page