Solved

Accessing Files - Win32

Posted on 1998-09-01
4
170 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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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 video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

760 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

18 Experts available now in Live!

Get 1:1 Help Now