Solved

using seek to read and write file lines

Posted on 1998-07-14
4
211 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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

615 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