in reply to Re: Determine whether file is dos or unix format
in thread Determine whether file is dos or unix format
Please use <c>...</c> around your code.
On which OS will your run your code?
Fix:
# Works on unix. # Works on Windows. # Still doesn't work on Mac. open(IN, '<', $input_file) or die "Couldn't open file $input_file: $!\n"; binmode(IN); while (<IN>) { if (/\x0D\x0A/) { die("DOS files not allowed\n"); } $total_lines++; }
Update: Here's something that works everywhere:
# Works on unix. # Works on Windows. # Works on Mac. { open(my $fh, '<', $input_file) or die "Couldn't open file $input_file: $!\n"; binmode($fh); my $buf = ''; while (read($fh, $buf, 1024, length($buf))) { if (/\x0D\x0A/) { die("DOS files not allowed\n"); } $buf = substr($buf, -1); } } { open(my $fh, '<', $input_file) or die "Couldn't open file $input_file: $!\n"; while (<$fh>) { $total_lines++; } }
|
|---|