Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

How do I seek to a certain position in a file?

( #9374=categorized question: print w/ replies, xml ) Need Help??
Contributed by Anonymous Monk on Apr 27, 2000 at 11:50 UTC
Q&A  > files


Description:

I would like to know how to go to a specific line in a file, followed by the specific char in that line. Could you explain more on the function fseek, and how to utilize it?

Answer: How do I seek to a certain position in a file?
contributed by btrott

Perl doesn't have fseek--it has seek, which does the same thing. As far as I know.

There really isn't a good way of jumping to a specific line in a file, unless your "lines" are fixed-length. This is because Perl (and your computer in general) thinks of files as a sequence of bytes, not as a sequence of lines.

There are exceptions (one being files with fixed-length records). Take a look at perlfaq5 and the DB_File manpage (in particular, you'll want to look at the DB_RECNO section).

One suggestion in perlfaq5 is to build an index of line numbers mapping to byte positions in the file. Then, when you need to jump to a particular line X, you read the byte position for line X from your index, then seek to that byte position.

This may work for you.

Answer: How do I seek to a certain position in a file?
contributed by turnstep

If you just want to find a certain line/character, try something like this:

$line=0; $matchline=5; $char = 1; open(MYFILE, "$myfile") || die; while(<MYFILE>) { ++$line >= $matchline || next; $l=length($_); print "Found line $line (length: $l)\n$_\n"; if ($l>$char) { print "Character $char is ", substr($_,$char-1,1), "\n"; } last; }
Answer: How do I seek to a certain position in a file?
contributed by DamnDirtyApe

To get to a known row in a text file, take a look at the Tie::File module on CPAN.

Answer: How do I seek to a certain position in a file?
contributed by simonflk

GhodMode said:

my @array = <FILE>; close FILE; # Print line 10 print $array[10];

Not quite. That prints line 11. I would do something like this:

my $findline = 10; while (<FILE>) { next unless $. == $findline; # .. do something last; #don't read any more }

For the original question "do something" would probably be a seek backwards to the desired char, or perhaps more sensibly, stop the line before and seek forwards.

for more info on $. and friends, see perldoc perlvar

-- simonflk

Answer: How do I seek to a certain position in a file?
contributed by GhodMode

open (FILE, "filename.txt") or die "Could not open filename.txt"; # Store the contents of the file in an array, one line per element my @array = <FILE>; close FILE; # Print line 10 print $array[10];

Please (register and) log in if you wish to add an answer



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others about the Monastery: (10)
    As of 2014-08-29 16:56 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The best computer themed movie is:











      Results (282 votes), past polls