Re: printing content of found file



Amichai Teumim 写道:
I'm trying to do the following:

I want to search for a specific file/s in my current dir and when it finds
it, print its contents. So I did the following:

#!/usr/bin/perl

please add 'use strict' and 'use warnings' at the the begin of a script.

opendir(CURRENT,".");
opendir CURRENT,"." or die $!;

@list = readdir(CURRENT);
closedir(CURRENT);

foreach $item (@list){
if($item =~ /notes/){


open(FILE,"@item");

I think you want to open the file $item,not the array of @item.If you 'use strict',you'll find this array was not declared before you use it.
so change it to:
open FILE,$item or die $!;


@file = <FILE>;
while(<FILE>){ print };

Since you've read all the content by the first statement,the file pointer has reached the end of file.So if you need to re-read it,please seek() it:
@file = <FILE>;
seek(FILE,0,0);
print while(<FILE>);

close(FILE);

print "@file\n";
}
}

Good luck!
.



Relevant Pages

  • Re: Casting a file pointer to char pointer
    ... I am reading a file into a char array. ... cast a file pointer to char pointer directly, ... You can cast a FILE * to a char * but doing so is meaningless. ...
    (comp.lang.c)
  • Re: Does array.read() move file pointer automatically?
    ... The readmethod has been deprecated since version Python 1.5.1. ... It will advance the file pointer. ... particular the "array" module). ... The following code generates an error (traceback message follows ...
    (comp.lang.python)
  • Re: Does array.read() move file pointer automatically?
    ... >> Lionel wrote: ... >>> read into "floatData" or must I move the file pointer by calling "seek ... > mistake somewhere in the way I'm importing and using modules (in ... > particular the "array" module). ...
    (comp.lang.python)
  • Re: Casting a file pointer to char pointer
    ... I am reading a file into a char array. ... Is there a technique through which i ... converted file pointer as an argument. ...
    (comp.lang.c)
  • Re: TWO loops and ONE if statement
    ... On 07/03/2007 02:53 AM, Amichai Teumim wrote: ... I need to sort the @array from lowest to highest using TWO loops and ...
    (perl.beginners)