我正在为我在编程课上的第一个作业编写一个简单的 perl 程序。我实际上已经被困在第一部分超过一天了。我无法让我的程序简单地打开与程序位于同一目录中的文本文件。
#!/usr/bin/perl -w
use strict;
my($fileName, $line);
print "Please enter name of file to be opened: ", "\n";
$fileName = <STDIN>;
chop($fileName);
#Associates FILE filehandle with the file: "filename.txt"
open(FILE, $fileName) or die("Can't open '$fileName': $!");
while(my $line = <FILE>){
print $line;
}
我正在使用草莓 perl。为了运行程序,我将程序拖放到命令行中以获取程序的地址。然后它尝试运行它。
它最初给了我一个关于关闭文件句柄错误的阅读线,然后我包含了 or die("Can't open '$fileName': $!");代码的一部分。
现在它说目录中没有这样的文件,但我知道 test.txt 文件在那里,因为我刚刚创建了它。
代码结果图片:http://imgur.com/R8s7FFE
显示我的文件位置的文件目录:http://imgur.com/nUfM4lA )
最佳答案
提示显示 C:\Users\jacjar\Documents
作为当前工作目录
所以这就是程序要寻找的地方 test.txt
但它不在那个目录中text.txt
在 L:\College\Junior Year\1st Semester\COSC 320 (Programming Languages)
移动您的 test.txt
文件到上面显示的 C: 路径,它将起作用
关于关闭文件句柄上的 Perl Readline - 文件不存在错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20036312/