Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

File seek

by Anonymous Monk
on Feb 25, 2013 at 10:04 UTC ( #1020483=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi

I have to find a particular keyword in file(<run>) and then truncate whatever is present after it and then seek to that position and start writing again on file.My code is as follows

$outputDir="$Location/statistic_file.xml"; open(OUTPUTFILE, '+<',$outputDir) or die "$outputDir : $!"; my $outputFileSize = -s $outputDir; if($outputFileSize != 0) { seek(CONFIRMEDOUTPUTFILE, -500, 2); my $lastChrs = <OUTPUTFILE>; my $Position = index($lastChrs, '<run>'); my $String = substr($lastChrs, $Position); $String =~ /Bytes=\"(\d+)\" Scanned=\"(\d+)\" total=\"(\d+)\"/; truncate CONFIRMEDOUTPUTFILE, $outputFileSize-length($String); seek(CONFIRMEDOUTPUTFILE, 0, 2); print CONFIRMEDOUTPUTFILE $temp; }

But it is not working.. What am I doing wrong??

Comment on File seek
Download Code
Re: File seek
by Anonymous Monk on Feb 25, 2013 at 10:15 UTC

    You're not error checking

    $ perl -le " open my($fh), 1 or die$!; seek $fh, 1,0 or die$!; " $ perl -le " open my($fh), 1 or die$!; seek $fh, -1,0 or die$!; " Invalid argument at -e line 1.

    You have the file size, if you want to seek to -500, do some math :)

      I am seeking to -500 as i know that the keyworn(<run>)wil be in last 500 characters

        I am seeking to -500 as i know that the keyworn(<run>)wil be in last 500 characters

        no you aren't, seek doesn't take negative offsets, like the error message I showed says

Re: File seek
by soonix (Curate) on Feb 25, 2013 at 10:49 UTC
    I wouldn't name an input file "OUTPUTFILE", but that doesn't disturb perl.
    The seek seeks from EOF, so negative offset is OK, if the file is longer than 500 bytes, so error checking would be better.

    However, you are opening OUTPUTFILE and then seeking on CONFIRMEDOUTPUTFILE...

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (6)
As of 2014-11-01 01:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (227 votes), past polls