Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Text file opening problem

by sufi (Initiate)
on Jan 04, 2011 at 06:53 UTC ( [id://880305]=perlquestion: print w/replies, xml ) Need Help??

sufi has asked for the wisdom of the Perl Monks concerning the following question:

I have problems in using data handler (In order to open a file and then perform some operations on it) My program:
open(FILE, "datebook.txt") || die "Can't open datebook: $!\n"; while(<FILE>) { print if /Sir Lancelot/; } close(FILE);
Error description:
Can't open datebook: No such file or directory

20110105 Janitored by Corion: Added formatting, code tags, as per Writeup Formatting Tips

Replies are listed 'Best First'.
Re: Text file opening problem
by GrandFather (Saint) on Jan 04, 2011 at 07:00 UTC

    Maybe the file or directory doesn't exist? Are you sure that the file is in the directory you think it is and that the script has permission to access it?

    As an aside, always use the three parameter version of open and use lexical file handles (you do use strictures - doc://strict]; use warnings; don't you). Your file open code should look like:

    open my $inFile, '<', "datebook.txt" or die "Can't open datebook.txt: +$!\n";

    Is the fact that your die mentions datebook but the open is on datebook.txt a typo or a bug?

    True laziness is hard work
Re: Text file opening problem
by syphilis (Archbishop) on Jan 04, 2011 at 07:07 UTC
    No such file or directory

    This simply means that there is no file named "datebook.txt" in the current working directory.
    Update: If you want to find out for sure what your "current working directory" is, then add this to the beginning of the script:
    use Cwd; print getcwd(), "\n";
    Cheers,
    Rob
Re: Text file opening problem
by samarzone (Pilgrim) on Jan 04, 2011 at 07:03 UTC

    The error clearly says that the file is not in the required path. To find the reason you can print the working directory just before opening the file. Or you can use absolute path (starting from root directory "/" in linux or from drive letter c:, d: etc. in windows) for the file name.

    You should also use <code></code> tags around the code you post. Read Markup in the Monastery

    --
    Regards
    - Samar

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://880305]
Approved by rovf
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2024-04-19 17:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found