Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: file basics

by repellent (Priest)
on Mar 11, 2012 at 22:33 UTC ( #959041=note: print w/ replies, xml ) Need Help??


in reply to file basics

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!


Comment on Re: file basics
Select or Download Code
Re^2: file basics
by harry1705 (Initiate) on Mar 12, 2012 at 01:15 UTC

    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.

        Thanks Monk, Both these ways helped me solve the problem, but when i tried without file extensions i got the error "Error in open:no such file or directory" by using either forward slashes or single quotes in the windows path

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://959041]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2014-07-26 01:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (175 votes), past polls