封闭文件句柄上的Perl Readline - 文件不存在错误 [英] Perl Readline on closed filehandle - file does not exist error
问题描述
我正在编写一个简单的perl程序,用于在我的编程类中的第一个任务。我已经被困在这个第一部分超过一天了。我无法让我的程序简单地打开与程序在同一目录中的文本文件。
I am working on a simple perl program for my first assignment in my programming class. I literally have been stuck on this first part for more than a day. I cannot get my program to simply open a text file that is in the same directory as the program.
#!/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。要运行程序,我将程序拖放到命令行中以获取程序的地址。然后尝试运行它。
I am using strawberry perl. To run the program I am dragging and dropping the program into the command line to get the address of the program. It then attempts to run it.
它最初给了我一个关于文件句柄错误的readline,然后我包括或者死(不能打开'$ fileName' :$!);部分代码。
It initially gave me a readline on closed filehandle error, and then I included the or die("Can't open '$fileName': $!"); portion of the code.
现在它表示目录中没有这样的文件,但是我知道test.txt文件在那里,因为我刚刚创建它。
Now it says that there is no such file at the directory, but I know that the test.txt file is there because I just created it.
代码结果图片: http://imgur.com/ R8s7FFE
显示文件位置的文件目录: http://imgur.com/nUfM4lA )
File directory that shows locations of my files: http://imgur.com/nUfM4lA)
推荐答案
提示符显示 C :\Users\jacjar\Documents
作为当前工作目录
The prompt is showing C:\Users\jacjar\Documents
as the current working directory
所以这是程序将寻找 test.txt
但它不在该目录中
text.txt
在 L:\College\Junior Year\1st Semester\COSC 320(编程语言)
将您的 test.txt
文件移动到上面显示的C:路径,它将工作
Move your test.txt
file to the C: path shown above and it will work
这篇关于封闭文件句柄上的Perl Readline - 文件不存在错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!