0

这就是我所拥有的。

#!/usr/bin/perl

$numbertotal = 0;
$filecount = 0;

open my $thisfile, '<', "files.txt" or die $!;

while (<$thisfile>) {
    ($thisdate, $thistime, $thisampm, $thissize, $thisname) = split;

    $numbertotal += $thissize;
    $filecount += 1;
    printf "%10d     %-25.25s\n", $thissize, $thisname;
}

$averagefilesize = $numbertotal / $filecount;

print "Total files:  ",$filecount,"      Average file size:  ",$averagefilesize," bytes\n";

我想采用 2 条不同的打印行并将它们发送到将由代码创建的另一个文件。我知道它将使用 '<' 操作,但我在弄清楚它时遇到了问题。

任何帮助表示赞赏。谢谢。

4

1 回答 1

1

要写入文件,请使用以下命令打开它'>'

open my $thisfile, '<', "files.txt" or die $!;
open my $thatfile, '>', 'output.txt' or die $!;

while (<$thisfile>) {
    ($thisdate, $thistime, $thisampm, $thissize, $thisname) = split;

    $numbertotal += $thissize;
    $filecount += 1;
    printf $thatfile "%10d     %-25.25s\n", $thissize, $thisname;
}
close $thatfile;
close $thisfile;
于 2013-11-04T17:08:02.903 回答