Solved

using seek to read and write file lines

Posted on 1998-07-14
4
210 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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

Suggested Solutions

Title # Comments Views Activity
hard perl script 16 168
Perl - Mawk 2 110
perl CPAN issue 3 131
syslog unix file 20 80
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…
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…
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…

752 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