?
Solved

using seek to read and write file lines

Posted on 1998-07-14
4
Medium Priority
?
220 Views
Last Modified: 2012-06-22
I have a file ie:
   line1
   line2
   line3
   line4
I would like to open the file, go directly to, say, line 3 and write something different in that location without disturbing the other lines.  The lines can be the same length.  I can use the seek function to go to that line, but my book doesn't say what code I need to read or write that line.  Do I open the file before using seek?  What code do I use to read or write that line?
0
Comment
Question by:donb1
  • 2
  • 2
4 Comments
 
LVL 85

Expert Comment

by:ozo
ID: 1208419
Yes, you open the file before you seek, what would you sek on therwise otherwise?
print FILE "line3\n"; #to write
$line = <FILE>; #to read
0
 
LVL 85

Expert Comment

by:ozo
ID: 1208420
See also "How do I randomly update a binary file?"
in
perldoc perlfaq5
0
 
LVL 5

Accepted Solution

by:
b2pi earned 300 total points
ID: 1208421
If you just want to read a particular line, AND all the lines are the same length, it's quite easy.

use strict;

my($LineLength);
open(FH, "a.txt);
$LineLength = <FH>;
close(FH);
$LineLength = length($LineLength);

print &ReadLine(2); ## Print the third line of the file
print &ReadLine(0); ## Print the first line of the file
print &ReadLine(4); ## Print the fifth line of the file.

sub ReadLine($) {
    my($line) = shift;

    open(FH, "<a.txt");      ## May be open, but this flushes
    seek(FH, $LineLength * $line, 0); ## 0 indicates absolute                                           ## position
    $line = <FH>;
    return $line;
}


If the lines aren't the same length, or you need to write, then I believe that the whole thing get's system dependent.  On some machines, you simply can't move the write pointer of a file without affecting the files contents.

A better way of approaching this might be to convert the file into a Berkely (or what have you) database, and then operate on it there.  You can then convert it back to a text file, if you choose.

perldoc DB_File

0
 
LVL 5

Expert Comment

by:b2pi
ID: 1208422
That would point you to using "+<filename".  Frankly, I feel that that is so inherently dangerous that it should almost not be allowed.

As the author (probably Tom Christenson) says:

     Locking and error checking are left as an exercise for the
     reader.  Don't forget them, or you'll be quite sorry.

That would be a non-trivial exercise.

0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

589 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question