Perl Readline on closed filehandle - file does not

2019-08-04 17:51发布

问题:

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?