Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: readdir <> attempted on invalid dirhandle DIR  (Read 12266 times)

0 Members and 1 Guest are viewing this topic.

sharoncatherine

  • Guest
readdir <> attempted on invalid dirhandle DIR
« on: September 27, 2011, 09:13:32 AM »
I am getting the this message when running the code below: readdir <> attempted on invalid dirhandle DIR at C:\Users\swalsh\Documents\read.pl

Basically I have roughly 3000 text file (each with the exact same format) and I need to parse each of these individually and get the program to return three pieces of information: FIPS, Total and Average. I have tried running it on a single txt file and it works but I now need it to run through the directory and give me back the same information from all the files. The txt files are stored in the C drive.

#!/usr/bin/perl
use warnings;
use strict;


my $logging = "file";

if ($logging eq "file") {
   open LOG, ">output.log" or die $!;
   select LOG;
}


opendir (DIR, "~/Users/swalsh/Documents/");
my @files = grep {/.txt/} readdir DIR;
close DIR;


foreach my $file (@files) {
   open (FH, "~/Users/swalsh/Documents/FIPS/");
   while (<FH>)   {
      print if ($_ =~/FIPS/);
      print if ($_ =~/Average/);
      print if ($_ =~/Total/);
}
   close (FH);
}

I hope you can help me. This is my first time attempting to program and I feel like I am very close to getting it right!

Geek-9pm


    Mastermind
  • Geek After Dark
  • Thanked: 1026
    • Gekk9pm bnlog
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 10
Re: readdir <> attempted on invalid dirhandle DIR
« Reply #1 on: September 27, 2011, 01:02:57 PM »
Welcome!
Computer Hope is the number one location for free computer help.
The forum will help everyone with all computer questions.

So far you program looks nice. But I am not able to tell your what went wrong. But here are some things that might help the forum.

Please specify what OS you are using. And maybe something about the hardware. Also, what programming language are you using? Which version?

If you can copy and past the exact error test, it really can help here.

Also, when posting code, use the CODE thing, if you please
Code: [Select]
This is nth CODE thing
Code looks nice when it
is in this
   Fixed font...
   mono spaced...
   terminal style
end of CODE thing

kpac

  • Web moderator


  • Hacker

  • kpac®
  • Thanked: 184
    • Yes
    • Yes
    • Yes
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 7
Re: readdir <> attempted on invalid dirhandle DIR
« Reply #2 on: September 27, 2011, 01:16:40 PM »
Quote
Also, what programming language are you using? Which version?
Uh, it's Perl, obviously.

Geek-9pm


    Mastermind
  • Geek After Dark
  • Thanked: 1026
    • Gekk9pm bnlog
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 10
Re: readdir <> attempted on invalid dirhandle DIR
« Reply #3 on: September 27, 2011, 01:59:29 PM »
Uh, it's Perl, obviously.
I did 'n want to make a wild guess.

BC_Programmer


    Mastermind
  • Typing is no substitute for thinking.
  • Thanked: 1140
    • Yes
    • Yes
    • BC-Programming.com
  • Certifications: List
  • Computer: Specs
  • Experience: Beginner
  • OS: Windows 11
Re: readdir <> attempted on invalid dirhandle DIR
« Reply #4 on: September 27, 2011, 04:36:22 PM »
I did 'n want to make a wild guess.
first line of code:

Code: [Select]
#!/usr/bin/perl

I was trying to dereference Null Pointers before it was cool.

Salmon Trout

  • Guest
Re: readdir <> attempted on invalid dirhandle DIR
« Reply #5 on: October 02, 2011, 02:18:11 AM »
Quote
C:\Users\swalsh\Documents\read.pl

Could this be a permissions problem?

[email protected]



    Newbie

    • Experience: Beginner
    • OS: Unknown
    Re: readdir <> attempted on invalid dirhandle DIR
    « Reply #6 on: April 12, 2012, 03:51:18 PM »
    The problem is simple enough,

    You are calling close(DIR) when you should call closedir(DIR)

    close works for files, closedir works for directories

    Allan

    • Moderator

    • Mastermind
    • Thanked: 1260
    • Experience: Guru
    • OS: Windows 10
    Re: readdir <> attempted on invalid dirhandle DIR
    « Reply #7 on: April 12, 2012, 04:00:22 PM »
    Since you're responding to a post that's 6 months old, I'll assume he figured it out by now.

    Geek-9pm


      Mastermind
    • Geek After Dark
    • Thanked: 1026
      • Gekk9pm bnlog
    • Certifications: List
    • Computer: Specs
    • Experience: Expert
    • OS: Windows 10
    Re: readdir <> attempted on invalid dirhandle DIR
    « Reply #8 on: April 12, 2012, 04:39:28 PM »
    Reading perl takes time.  8)