我一直在尝试查找与文件处理相关的示例,但我找不到任何可以解决我问题的方法。
use strict;
use warnings;
my ($m, $b) = @ARGV;
my $count_args = $#ARGV + 1;
my $times = 2**$m;
my $output = "main fucntion to be called???\n";
open(OUTFILE, "> output1.txt") || die "could not open output file";
print OUTFILE "$output\n"; ## Notice, there is not a comma after the file handle and the string to be output
close(OUTFILE);
main();
sub main {
if ($m =~ /^\d+$/) {
if ($b =~ /^and$/i) {
func_and();
}
else {
print " not supported\n";
}
}
else {
print " enter valid number of pins\n";
}
}
sub func_and {
my $bit;
for (my $i = 0 ; $i < $times ; $i++) {
my $p = sprintf("%0${m}b", $i);
print "$p\t";
my @c = split('', $p);
my $result = 100;
foreach $bit (@c) {
if ($result < 100) {
$result = $result && $bit;
}
else {
$result = $bit;
}
}
print "result is $result \n";
}
}
如果我将输入设为2并且输出打印在屏幕上,则此程序打印输出。
我想更改此程序的文件句柄STDOUT
,我想将输出打印到output1.txt文件
你能指出我犯的错误吗? -----&GT;
open (my $STDOLD, '>&', STDOUT);
# redirect STDOUT to log.txt
open (STDOUT, '>>', "$ARGV[1]".".log");
print main();
# restore STDOUT
open (STDOUT, '>&', $STDOLD);
print " check .log file\n This should show in the terminal\n";
这对我有用..但是在日志文件的末尾我打印了数字“1”我不知道为什么..我相信它是打印的时间戳。我需要删除它..你知道它为什么会发生吗?
答案 0 :(得分:1)
听起来您想将程序的输出重定向到文件?
要从命令行执行此操作,请输入
perl myprogram.pl > myoutput.txt
如果您希望打印到STDOUT
的所有内容都转到某个文件,那么只需在程序开头将其作为文件句柄打开。
喜欢这个
open STDOUT, '>', 'myoutput.txt' or die $!;
答案 1 :(得分:0)
您分配给typeglob *::STDOUT
的任何文件句柄都将成为STDOUT。
if ( my $fh = FileHandle->new( ">>$stdout_path" )) {
*::STDOUT = $fh;
}
虽然我通常只将STDERR重定向到STDOUT或文件。
答案 2 :(得分:0)
我会尝试以下方法(避免使用双引号加上使用“clear then append”方法:
:
:
my $output = "main fucntion to be called???\n";
open(OUTFILE, '> output1.txt') || die "could not clear down output file";
close OUTFILE;
open(OUTFILE, '>> output1.txt') || die "could not open append output file";
print OUTFILE "$output\n"; ## Notice, there is not a comma after the file handle and the string to be output
close OUTFILE;
:
:
在密切的陈述中不需要括号。第一次打开有效地清空文件。第二个是开放式(或附加)。 我假设你将使用print OUTFILE ...并移动第二个关闭输出;到你的剧本结尾。
... OR
:
:
my $output = "main fucntion to be called???\n";
open(STDOUT, '> output1.txt') || die "could not clear down output file";
close STDOUT;
open(STDOUT, '>> output1.txt') || die "could not open append output file";
print "$output\n"; ## Notice, there is not a comma after the file handle and the string to be output
:
:
close STDOUT;
:
:
非常确定这是有效的 - 不需要在每个打印命令上使用STDOUT。
答案 3 :(得分:0)
根据我的理解,您正在尝试将“打印”的所有内容发送到文件中。如果那是你想要做的,那就很简单了。
open (OUTPUT, '>output1.txt');
#whatever logic you want to do
print OUTPUT "whatever you want to print","temporary outputs";
#any other logic you have for computing the output
print OUTPUT "output1","output2";
#after completing all print statements, close the file handle
close(OUTPUT);
基本上,您需要将文件句柄(在我的示例中为OUTPUT)设置为“打印”,然后像平常一样打印。例如,如果您的原始行是
print "not supported\n";
它将成为
print OUTPUT "not supported\n";
答案 4 :(得分:0)
您可以使用select
设置打印的默认文件句柄:
open(my $OUTFILE, '>', 'output1.txt') || die "could not open output file: $!";
select $OUTFILE;
print $output;