Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Goto a line in a file

Status
Not open for further replies.

mbrohm

Programmer
Sep 18, 2002
5
US
I have a program that reads in from a file and ftp's each line. This is a huge file and sometimes the ftp connection is lost and the program restarts and starts at the first line. I was going to read this file into an array and when the connection is lost go to that place in the array and start from there. Is there a better way to do this? I am new to perl and I know C++ allows you to place a pointer in the file and next time you open the file go straight to that pointer, can you do this in perl? Any information would be helpful.

Thanks
 
Hi there,

You can't do this in *quite* the way you're asking -- but we can work around that I think, something like this should do what you want.

use strict;
use warnings;

my $req_line_no = 2654; # or whatever line you want
my $file = 'a_file.txt';

open(F, $file) || die "error opening $!\n";
while(<F>){
next if $. < $req_line_no;
# now process the rest of your input file
}
Mike
________________________________________________________________

&quot;Experience is the comb that Nature gives us, after we are bald.&quot;

Is that a haiku?
I never could get the hang
of writing those things.
 
Why don't you read in the original file at the beginning, and then at the end after the connection is lost, write back over the file with what's not processed. You can even dump into a &quot;completed&quot; file what was done.

Example:
my $file_not_done = 'path_and_file';
my $file_completed = 'path_and_file';

local(*FILE1, *FILE2);
my (@file1_lines);

open(FILE1, $file_not_done);
@file1_lines = <FILE1>;
chomp(@file1_lines);
close(FILE1);

for (my $i=0; $i < @file1_lines; $i++) {
# FTP $file1_lines[$i] like normal
# ...

last if (!$successful);
}

# Write out files

# Write out original file
open(FILE1, &quot;>$file_not_done&quot;);
map {print FILE1 &quot;$file1_lines[$_]\n&quot;} ($i..$#file1_lines);
close(FILE1);

# Write out completed file
open(FILE2, &quot;>$file_completed&quot;);
map {print FILE2 &quot;$file1_lines[$_]\n&quot;} (0..($i-1));
close(FILE2);

-Aaron Simmons
asimmons@mitre.org

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top