At 17:25 +0000 28/2/06, Adam Witney wrote:
Does this work on all platforms? When I try it it works fine on OSX/Linux
with MAC/DOS/UNIX line endings, but fails (reads the whole file) when
reading DOS line endings on WinXP... Here is my script
use Fcntl;
my $file = $ARGV[0];
open(INFILE, $file) || die "cannot open $file: $!\n";
{
local $/ = get_line_ending_for_file($file);
Try reading the line ending before opening the file, ie:
my $temp_line_ending = get_line_ending_for_file($file);
open(INFILE, $file) || die "cannot open $file: $!\n";
{
local $/ = $temp_line_ending;
It may be that WinXP is getting confused by opening the file, and
then sysopen/closing the file in get_line_ending_for_file, and then
expecting to be able to read from the file. Not all platforms allow
you to open the same file multiple times and have independent access
to it - not that I know anything about WinXP, but old Classic Mac OS
would quite probably have had problems with this.
Enjoy,
Peter.
while(<INFILE>)
{
my $line = $_;
chomp $line;
print "\n\n".length($line)."\n\n";
last;
}
}
sub get_line_ending_for_file {
my($file) = @_;
my $fh;
sysopen( $fh, $file, O_RDONLY );
sysread( $fh, $_, 200 );
close( $fh );
return /(\015\012|\015|\012)/ ? $1 : "\n";
}
Thanks for any help
Adam
--
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.
--
<http://www.stairways.com/> <http://download.stairways.com/>