Carl Johnson wrote:
Hello group,
Hello,
I'm trying to read the directory "C:\GIS\wrapped_data" and write record.
My scripts is erroring with "can't open directory: no such file or
directory. What am I missing?
$outfile = "$infile.txt";
my $dir = "C:\GIS\wrapped_data";
Perl's double quoted strings interpolate their contents so scalar and array
variables are expanded and escape (backslash-character) sequences are
converted to their special characters. You should use forward slashes in file
paths.
opendir(DIR,"$dir") or die "Can't open $dir directory: $!";
Your error message should have printed out the contents of $dir which would
have hinted at the erroneous path name.
perldoc -q quoting
open (OUT, ">$outfile") or die "Error, cannot open file: $outfile. $!";
$record = "";
$index=0;
while ( $numbytes = read(DIR, $record, 1200) ) {
You cannot read from a directory handle with read(). Only functions with
'dir' in their names can use a directory handle.
perldoc -f opendir
perldoc -f readdir
perldoc -f rewinddir
perldoc -f seekdir
perldoc -f telldir
perldoc -f closedir
$index++;
if ($numbytes == 1200) {
Although you are telling read() that you want 1200 bytes that is only a
suggestion and read() could return anything from 1 to 1200 bytes and still be
valid. By testing for exactly 1200 bytes you could miss some data.
perldoc -f read
[snip]
Attempts to read LENGTH characters of data into variable SCALAR from
^^^^^^^^
the specified FILEHANDLE. Returns the number of characters actually
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
read, 0 at end of file, or undef if there was an error (in the
^^^^
latter case $! is also set). SCALAR will be grown or shrunk so that
the last character actually read is the last character of the scalar
after the read.
print OUT "$record";
} else {
die "File Read Error on record $index: $numbytes bytes read; Should be 1200.\n";
}
}
close DIR;
close OUT;
John
--
use Perl;
program
fulfillment
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>