open for writing without immediately erasing

M

mike

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.

--
 
D

Darren Dunham

Nathan Keel said:
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.
 
N

Nathan Keel

mike said:
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.
 
U

Uri Guttman

CM> I recommend Tie::File.

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

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
 
D

Dr.Ruud

mike said:
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?)
 

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

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top