Reputation: 401
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: https://i.sstatic.net/dlK5g.jpg
File directory that shows locations of my files: https://i.sstatic.net/d3FSK.jpg)
Upvotes: 2
Views: 6804
Reputation: 34327
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
Upvotes: 2
Reputation: 385789
Do you realize you are trying to open C:\User\jacjar\Documents\test.txt
?
Upvotes: 1