Solved

Accessing Files - Win32

Posted on 1998-09-01
4
212 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

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

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
Six Sigma Control Plans

729 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