Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re^2: Empty File Contents before writing

by Athanasius (Abbot)
on Feb 19, 2013 at 08:01 UTC ( #1019477=note: print w/ replies, xml ) Need Help??


in reply to Re: Empty File Contnts before writing
in thread Empty File Contnts before writing

Yes, seek works in this case because the final output is longer than the initial output. But a word of caution is in order. Consider:

#! perl use strict; use warnings; my $filename = 'test.txt'; open(my $fh, '>', $filename) or die "Cannot open file '$filename' for +writing: $!"; print $fh "The quick brown fox jumped over the unfortunate dog."; seek $fh, 0, 0; print $fh "The final output."; close($fh) or die "Cannot close file '$filename': $!";

This results in file “test.txt” containing:

The final output.ox jumped over the unfortunate dog.

as seeking to the beginning of the file does not erase its existing contents.

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,


Comment on Re^2: Empty File Contents before writing
Select or Download Code
Re^3: Empty File Contents before writing
by gautamparimoo (Beadle) on Feb 19, 2013 at 09:47 UTC

    So I will have to open and close the file agin in order to completely truncate watever is written in previous write attempts?

      There is also a built-in function called truncate that will do what you want.

      $ perl -Mstrict -Mwarnings -e ' > open my $fh, q{>}, q{rubbish} or die $!; > print $fh qq{A longish line of text\n}; > truncate $fh, 0 or die $!; > print $fh qq{Short\n}; > close $fh or die $!;' $ cat rubbish Short $

      Update: Added example code.

      Cheers,

      JohnGG

        The help for truncate says

        The position in the file of FILEHANDLE is left unchanged. You may want + to call seek before writing to the file.

        So, any ideas why your code works without needing a seek 0?

Re^3: Empty File Contents before writing
by sundialsvc4 (Abbot) on Feb 19, 2013 at 13:56 UTC
    Yeah, gotta watch out for those output-oxen. ;-)

      So I need to open and close my file every time I print something to it so that it overwrites the previous print in the file?? Is there any other way coz I would have open and close the file numerous times.

        No, you can use truncate and seek and if you put them together in a subroutine you only have to make one call each time to empty your file ready for the next print statement. Note that /usr/bin/hexdump run on an empty file produces no output.

        $ perl -Mstrict -Mwarnings -e ' > sub truncZero > { > my $fh = shift; > truncate $fh, 0 or die $!; > seek $fh, 0, 0 or die $!; > } > > open my $fh, q{>}, q{rubbish} or die $!; > > print $fh qq{Mary had a little lamb\n}; > print qx{/usr/bin/hexdump -vC rubbish}, qq{-----\n}; > > truncZero( $fh ); > print qx{/usr/bin/hexdump -vC rubbish}, qq{-----\n}; > > print $fh qq{The boy stood on the burning deck\n}; > print qx{/usr/bin/hexdump -vC rubbish}, qq{-----\n}; > > truncZero( $fh ); > print qx{/usr/bin/hexdump -vC rubbish}, qq{-----\n}; > > print $fh qq{I met a traveller from an antique land\n}; > print qx{/usr/bin/hexdump -vC rubbish}, qq{-----\n}; > > close $fh or die $!;' 00000000 4d 61 72 79 20 68 61 64 20 61 20 6c 69 74 74 6c |Mary had +a littl| 00000010 65 20 6c 61 6d 62 0a |e lamb.| 00000017 ----- ----- 00000000 54 68 65 20 62 6f 79 20 73 74 6f 6f 64 20 6f 6e |The boy s +tood on| 00000010 20 74 68 65 20 62 75 72 6e 69 6e 67 20 64 65 63 | the burn +ing dec| 00000020 6b 0a |k.| 00000022 ----- ----- 00000000 49 20 6d 65 74 20 61 20 74 72 61 76 65 6c 6c 65 |I met a t +ravelle| 00000010 72 20 66 72 6f 6d 20 61 6e 20 61 6e 74 69 71 75 |r from an + antiqu| 00000020 65 20 6c 61 6e 64 0a |e land.| 00000027 ----- $ cat rubbish I met a traveller from an antique land $

        I hope this is helpful.

        Cheers,

        JohnGG

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1019477]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2015-07-04 22:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (60 votes), past polls