Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

file basics

by harry1705 (Initiate)
on Mar 11, 2012 at 22:21 UTC ( [id://959039]=perlquestion: print w/replies, xml ) Need Help??

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

I am a perl beginner,I am working on windows7 usin strawberry perl, I am trying to open a file using

#opening a file, checking the error code. $fname="/home/isg/report.txt"; open xyz, $fname or die "Error in open:$!"; while(<xyz>){ print "line number $. is : $_"; }
If i had to open some file from say C:\perl_tests\test.txt, what should be the string $fname

Replies are listed 'Best First'.
Re: file basics
by repellent (Priest) on Mar 11, 2012 at 22:33 UTC
    What have you tried? First thing that comes to mind is to replace $fname with 'C:\perl_tests\test.txt'. Let's see what happens:
    C:\Users\repellent>type C:\perl_tests\test.txt abc cde efg C:\Users\repellent>type t.pl #opening a file, checking the error code. $fname='C:\perl_tests\test.txt'; open xyz, $fname or die "Error in open:$!"; while(<xyz>){ print "line number $. is : $_"; } C:\Users\repellent>perl t.pl line number 1 is : abc line number 2 is : cde line number 3 is : efg

    Hey, it just works!

      Thank you Mr.Repellent;At first i tried it with a file which already existed in the system thinking that it was a file with .txt extension.And then i got the error "file does not exist", so i decided to seek some help and hence posted my question modified, Got reply from you and tried to figure what was wrong, I found that the file which i used didn't have any extension on it. It now works.Thanks for enlightening me with What have you tried? and also for answering my dumb question

        The problem you probably ran into was that you used double quotes for your file name. (File extensions probably had nothing to do with it.) Since Windows's path separators are backslashes, and backslashes are a special character inside double quotes, this is many a beginner's stumbling-block. There are two easy fixes: use forward slashes (they work even on Windows), or use single quotes.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (7)
As of 2024-04-19 15:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found