Solved

Accessing Files - Win32

Posted on 1998-09-01
4
191 Views
Last Modified: 2010-03-05
I am having trouble figuring out how to read a file using Perl for Win32.  Here is an example of my code.

$FileName = "d:\Perl\Scripts\testfile.txt"
if ( open ( eFile, $FileName )) {
        $line = <eFile>;
        while ( $line ne "" ) {
           print ( $line );
           $line = <eFile>;
        }
}
close ( eFile );

I have tried several things based on examples from the Learning Perl on Win32 Systems but have had no success.  

Can someone offer some help?

Thanks in advance.
0
Comment
Question by:tyates
  • 2
4 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 1204659
$FileName = 'd:\Perl\Scripts\testfile.txt';
if( open ( eFile, $FileName ) ){
  ...
}else{
  warn "can't open $FileName because $!";
}
0
 

Expert Comment

by:kaijen
ID: 1204660
Hi,

you could also use '/' (Unix-Style) instead of the bckslash.

This would make:

$FileName = 'd:/Perl/Scripts/testfile.txt';
if ( open ( eFile, $FileName ) )
{
  while ( ( $line = <eFile> ) ne "" )
  {
    print $line;
  }
}
close ( eFile );

or do you want:

  while ( $line = <eFile> )
  {
    print $line if $line ne "";
  }

Best regards,
Kai.
0
 
LVL 84

Expert Comment

by:ozo
ID: 1204661
#or just
print while( <eFile> );
0
 

Accepted Solution

by:
nchen091098 earned 50 total points
ID: 1204662
Your filename needs to escape out the backslashes.

For your first line, use this instead:
$FileName = "d:\\Perl\\Scripts\\testfile.txt";

Otherwise, "\P", "\S", etc will be considered individual characters
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.

815 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now