Solved

using seek to read and write file lines

Posted on 1998-07-14
4
203 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 84

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 84

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 100 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
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…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

758 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now