[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] Read entire file?



At 11:00 am -0500 27/12/00, Jim Correia wrote:
>So I'm a c programmer, and my perl book is at the office.
>
>What is the best (fastest, clearest code) to read an entire file into a
>variable?  I want to preserve whatever line endings were in the original
>file so I don't want to read it by lines.  Right now I am reading by
>chunks because I can't seem to find a function that will return the size
>in bytes of the file on disk...

	stat

Returns a 13-element array giving the status info for a file, either the
file opened via FILEHANDLE, or named by EXPR.  If EXPR is omitted, it stats
$_.  Returns a null list if the stat fails.  Typically used as follows:

($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
$atime,$mtime,$ctime,$blksize,$blocks)
= stat($filename);



#( stat $fin and stat FIN both seem to give the result)
$dir = 'd:documents';
$fin = "$dir:temp.in";
$fout = "$dir:temp.out";
open FIN, "<$fin";
open FOUT, ">$fout";
@stat = stat $fin;
$bytes =  $stat[7];
print "\nSize of \"$fin\": $bytes bytes.\n";
while (read FIN, $_, $bytes) {
	print; print "\n\n";
	print FOUT;
}
close FIN;
close FOUT;

JD





# ===== Want to unsubscribe from this list?
# ===== Send mail with body "unsubscribe" to macperl-request@macperl.org