?
Solved

Accessing Files - Win32

Posted on 1998-09-01
4
Medium Priority
?
213 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 100 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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…
Six Sigma Control Plans
Suggested Courses

762 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