Perl Readline on closed filehandle - file does not exist error

天大地大妈咪最大 提交于 2019-12-11 10:29:53

问题


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;
}

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.

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.

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.

Picture of the code results: http://imgur.com/R8s7FFE

File directory that shows locations of my files: http://imgur.com/nUfM4lA)


回答1:


The prompt is showing C:\Users\jacjar\Documents as the current working directory

So this is where the program will look for test.txt

But it is not in that directory

text.txt is in L:\College\Junior Year\1st Semester\COSC 320 (Programming Languages)

Move your test.txt file to the C: path shown above and it will work




回答2:


Do you realize you are trying to open C:\User\jacjar\Documents\test.txt?



来源:https://stackoverflow.com/questions/20036312/perl-readline-on-closed-filehandle-file-does-not-exist-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!