Chinaunix

标题: 请问:如何读取文件的最后一行? [打印本页]

作者: youhaorenshi    时间: 2005-08-24 14:24
标题: 请问:如何读取文件的最后一行?
请问:在Windows环境下,怎么直接读取文件的最后一行的内容?因为文件很大,每行至少1024个字符,但是只有文件的最后一行才是我需要的。我用seek可以移到文件末尾,然后再用readline,但是内容是空。
作者: 兰花仙子    时间: 2005-08-24 15:01
标题: 请问:如何读取文件的最后一行?
[quote]原帖由 "youhaorenshi"]请问:在Windows环境下,怎么直接读取文件的最后一行的内容?因为文件很大,每行至少1024个字符,但是只有文件的最后一行才是我需要的。我用seek可以移到文件末尾,然后再用readline,但是内容是空。[/quote 发表:



open (FILE,"file.txt" or die "$!";
my @arr=<FILE>;;
close FILE;
my $last=$arr[$#arr];

$last里就是最后一行的内容了。
作者: youhaorenshi    时间: 2005-08-24 15:13
标题: 请问:如何读取文件的最后一行?
楼上的方法我也知道,但是要是一个文件几百兆,就太慢了。有没有别的方法了。或者有什么办法可以知道文件有多少行。
作者: apile    时间: 2005-08-24 15:17
标题: 请问:如何读取文件的最后一行?
http://search.cpan.org/~mjd/Tie-File-0.96/lib/Tie/File.pm
DESCRIPTION ^

Tie::File represents a regular text file as a Perl array. Each element in the array corresponds to a record in the file. The first line of the file is element 0 of the array; the second line is element 1, and so on.

The file is not loaded into memory, so this will work even for gigantic files.

Changes to the array are reflected in the file immediately.

Lazy people and beginners may now stop reading the manual.
作者: 兰花仙子    时间: 2005-08-24 15:25
标题: 请问:如何读取文件的最后一行?
[quote]原帖由 "youhaorenshi"]楼上的方法我也知道,但是要是一个文件几百兆,就太慢了。有没有别的方法了。或者有什么办法可以知道文件有多少行。[/quote 发表:

  1. open (FILE,"file.txt") or die "$!";
  2. while (<FILE>;)
  3. {
  4.     open (TMP,">;","tmp.txt") or die "$!";
  5.     print TMP $_;
  6.     close TMP;
  7. }
  8. close FILE;
复制代码


如上,用交换文件也可以,如果你内存有限的话。
作者: yearnx    时间: 2005-08-24 17:12
标题: 请问:如何读取文件的最后一行?
能不能配合shell的tail -1 file.txt来呢?在windows下也可以模拟个shell的
作者: orangetouch    时间: 2005-08-25 01:02
标题: 请问:如何读取文件的最后一行?
打开文件,把指针移动到最后面,一个一个字节往前读,直到读到\N为止
作者: 笨狗    时间: 2005-08-25 03:33
标题: 请问:如何读取文件的最后一行?
原帖由 "apile" 发表:
http://search.cpan.org/~mjd/Tie-File-0.96/lib/Tie/File.pm
DESCRIPTION ^

Tie::File represents a regular text file as a Perl array. Each element in the array corresponds to a record in the file. The..........

楼主试试这个
作者: run309    时间: 2005-08-25 11:36
标题: 请问:如何读取文件的最后一行?
open (FILE,"file.txt" ;
$a=<FILE>;;push(@a,$a);
while (<FILE>
{ push(@a,$_);
shift@a;
}
print @a;
close FILE;
作者: yearnx    时间: 2005-08-26 14:24
标题: 请问:如何读取文件的最后一行?
原帖由 "run309" 发表:
open (FILE,"file.txt" ;
$a=<FILE>;;push(@a,$a);
while (<FILE>
{ push(@a,$_);
shift@a;
}
print @a;
close FILE;



糊弄人啊~说明白点阿~~
作者: rorot    时间: 2005-08-26 21:40
标题: 请问:如何读取文件的最后一行?
[quote]原帖由 "orangetouch"]打开文件,把指针移动到最后面,一个一个字节往前读,直到读到\N为止[/quote 发表:


同意orangetouch的做法. Tie::File虽然没有把文件读入, 但是要得到Tie::File出来的$#array需要搜索'\n', 这样需要遍历文件. 所以我同意把指针移动到句柄末尾,倒着读的作法.


  1. #!/usr/bin/perl -w

  2. use strict;

  3. my $file = shift or die;
  4. my $content = "";

  5. open (F, $file) or die $!;
  6. seek (F, 0, 2);                                 # set handler at the end of $file
  7. until ($content =~ m/\n(.*)\n?$/)
  8. {
  9.         my $string;
  10.         if (seek (F, -1024, 1))              # backward 1024 bytes
  11.         {
  12.                 my $n = read (F, $string, 1024) or die $!;
  13.                 $content = $string . $content;
  14.                 last if ($n < 1024);
  15.                 seek(F, -1024, 1);
  16.         }
  17.         else
  18.         {
  19.                 my $len = tell F;
  20.                 seek (F, 0, 0) || die "see error at $file";
  21.                 read (F, $string, $len) or die $!;
  22.                 $content = $string . $content;
  23.                 last;
  24.         }
  25. }
  26. close(F);

  27. if ($content =~ m/\n\n$/)
  28. {
  29.         print "\n";
  30. }
  31. elsif ($content =~ m/\n?(.*)\n?$/)
  32. {
  33.         print "$1\n";
  34. }
  35. else
  36. {
  37.         print $content, "\n";
  38. }
复制代码

作者: run309    时间: 2005-08-29 09:32
标题: 请问:如何读取文件的最后一行?
run309 写到:
open (FILE,"file.txt" ;
$a=<FILE>;;push(@a,$a);
while (<FILE>
{ push(@a,$_);
shift@a;
}

close FILE;
print @a;
只要修改目录和文件名即可,有可能你的文件末尾是空行,最好先删除。




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2