By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,490 Members | 1,244 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,490 IT Pros & Developers. It's quick & easy.

File Descriptor Errors in Active State Perl

P: 8
I am new to using Active State Perl, I've use Perl on Linux and Unix systems and the same thing on unix systems don't work with active state and I am perplexed. I wrote the code below and I am getting seven loops of Can not open shawneechamber.txt for reading: Bad file descriptor.

Has anyone had this problem? I am on xp and installed v5.8.7 build for MSWIN32-x86

I am having the same problem at work and at home. I thank you for your help in advance.

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. use warnings;
  3. use strict;
  4. #define variables
  5. my $File = 'shawneechamber.txt';
  6. #my $OutFilePath = 'C:\PerlApp\DataOut\';
  7. #my $InFilePath = 'C:\PerlApp\DataIn\';
  8. #my $OutFileExt = ".csv";
  9. my $lineno = 1;
  10. #my @indata;
  11.  
  12. #open file for reading
  13. open (FILE, "<$File") or die ("Can not open $File for reading:  $!.\n");
  14. binmode(FILE);
  15. #@indata = <FILE>;
  16. while (<FILE>) {
  17. print $lineno++;
  18. print "Can not open $File for reading:  $!.\n";
  19. }
  20. close FILE;
  21.  
Sep 10 '07 #1
Share this Question
Share on Google+
8 Replies


numberwhun
Expert Mod 2.5K+
P: 3,503
I am new to using Active State Perl, I've use Perl on Linux and Unix systems and the same thing on unix systems don't work with active state and I am perplexed. I wrote the code below and I am getting seven loops of Can not open shawneechamber.txt for reading: Bad file descriptor.

Has anyone had this problem? I am on xp and installed v5.8.7 build for MSWIN32-x86

I am having the same problem at work and at home. I thank you for your help in advance.

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. use warnings;
  3. use strict;
  4. #define variables
  5. my $File = 'shawneechamber.txt';
  6. #my $OutFilePath = 'C:\PerlApp\DataOut\';
  7. #my $InFilePath = 'C:\PerlApp\DataIn\';
  8. #my $OutFileExt = ".csv";
  9. my $lineno = 1;
  10. #my @indata;
  11.  
  12. #open file for reading
  13. open (FILE, "<$File") or die ("Can not open $File for reading:  $!.\n");
  14. binmode(FILE);
  15. #@indata = <FILE>;
  16. while (<FILE>) {
  17. print $lineno++;
  18. print "Can not open $File for reading:  $!.\n";
  19. }
  20. close FILE;
  21.  
Well, just to note, in the Windows world, you do not need the shebang line in your regular scripts. It is ignored by the Windows version of Perl.

It looks like your open statement is defined correctly, but considering the error, I am wondering if the file is there or accessible. Check the path to it (it is defined as current directory) and check permissions to see if you can read it.

Regards,

Jeff
Sep 10 '07 #2

P: 8
I have full access to the shawneechamber.txt file.
Sep 10 '07 #3

numberwhun
Expert Mod 2.5K+
P: 3,503
I have full access to the shawneechamber.txt file.
Ok, well it is something to do with where the file is then. I ran code very similar to yours and it worked fine. I cannot see your system to see what's wrong, unfortunately.

Is the file in the same directory as the script? If not, then you need to specify the full path to the file.

Regards,

Jeff
Sep 10 '07 #4

P: 8
The file is in the exact same sub directory as the script.

I also re-ran it with absolute path and I got the same error.
Sep 10 '07 #5

numberwhun
Expert Mod 2.5K+
P: 3,503
The file is in the exact same sub directory as the script.

I also re-ran it with absolute path and I got the same error.

I am wondering if you are actually seeing the error you think you are. I ask because you have the following print statement inside of your while loop:

Expand|Select|Wrap|Line Numbers
  1. print "Can not open $File for reading:  $!.\n";
  2.  
Try taking that out and re-run it and see what happens.

Regards,

Jeff
Sep 10 '07 #6

P: 8
That was it. Thanks Jeff.
Sep 10 '07 #7

numberwhun
Expert Mod 2.5K+
P: 3,503
That was it. Thanks Jeff.
Your welcome! And by the way, Welcome to TSDN!

Regards,

Jeff
Sep 10 '07 #8

KevinADC
Expert 2.5K+
P: 4,059
There is no need to increment a scalar to know which line of the file is being operated on, perl is already doing that in the background with the special variable $. (dollar sign dot)

Expand|Select|Wrap|Line Numbers
  1. open (FILE, "<$File") or die ("Can not open $File for reading:  $!.\n");
  2. while (<FILE>) {
  3.    print "Line number: $.\n";
  4. }
  5. close FILE;
$. is a read-only value.
Sep 10 '07 #9

Post your reply

Sign in to post your reply or Sign up for a free account.