【问题标题】:Perl Readline on closed filehandle - file does not exist error关闭文件句柄上的 Perl Readline - 文件不存在错误
【发布时间】:2013-11-30 22:13:19
【问题描述】:

我正在为我在编程课上的第一个作业编写一个简单的 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。为了运行程序,我将程序拖放到命令行中以获取程序的地址。然后它会尝试运行它。

它最初给了我一个关于关闭文件句柄错误的 readline,然后我包含了 or die("Can't open '$fileName': $!");部分代码。

现在它说目录中没有这样的文件,但我知道 test.txt 文件在那里,因为我刚刚创建了它。

代码结果图片:http://imgur.com/R8s7FFE

显示我的文件位置的文件目录:http://imgur.com/nUfM4lA)

【问题讨论】:

  • 您确定您在正确的目录中吗? use Cwd; print getcwd(), "\n";(或者只输入echo %cd% |dir | 作为你的文件名——但你真的应该使用带有显式模式的three-argument form of open() 来防止这种伎俩。)

标签: perl file directory readline


【解决方案1】:

提示显示C:\Users\jacjar\Documents为当前工作目录

所以程序将在此处查找test.txt

但它不在那个目录中

text.txtL:\College\Junior Year\1st Semester\COSC 320 (Programming Languages)

将您的test.txt 文件移动到上面显示的 C: 路径,它会起作用

【讨论】:

  • 要更改 cmd shell 中的工作目录,请使用 cd 命令。要更改 perl 脚本中的工作目录,请使用 chdir
  • 是的,不幸的是,这是 Windows 的“功能”。不过,它可以被禁用。 “工具” | “文件夹选项” | “观点” |取消选中“隐藏已知文件类型的扩展名”| “申请” | “应用到文件夹”
【解决方案2】:

您是否意识到您正在尝试打开C:\User\jacjar\Documents\test.txt

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-07
    相关资源
    最近更新 更多