你可以告诉我在下面的代码中要纠正什么,这样它就不会替换我文件的第一行,而是告诉我追加的换行符吗?
for ($count = 10; $count >= 1; $count--) {
my $newline = "text";
my $file1 = "C:/Users/";
my $file = $file1 . $count . ".txt";
local @ARGV = ($file);
local $^I = '.bac';
while (<>) {
if ($. == 1) {
print "$newline$/";
}
else {
print;
}
}
}
编辑:已解决。解决方案在Perl FAQ
中open my $in, '<', $file or die "Can't read old file: $!";
open my $out, '>', "$file'" or die "Can't write new file: $!";
print $out $newline;
while(<$in>) {
print $out $_;
}
close $out;