APM: chokes on dos formatted csv file

Tim McDaniel tmcd at panix.com
Wed Sep 24 10:13:12 PDT 2008


On Wed, 24 Sep 2008, Keith Howanitz <howanitz at gmail.com> wrote:
> This sample runs great if the file is Unix formatted, but if I upload
> a file from a windows system it dies with error:
> Error getting csv data. (Bad file descriptor) at ./test-csv2 line 13,
> <MYCSV> line 1.
>
> Any suggestions?

See what happens if you strip carriage returns yourself?  I don't see
how it can hurt with any reasonable file.

6> chomp (my $FN = $ARGV[0]);

You shouldn't have to chomp a command-line argument.  chomp() gets rid
of a trailing line-terminator character, and it takes a special effort
to get one of those in even in a Linux environment.

11> while (<MYCSV>) {
12>  chomp;
13> $csv->parse($_)
14>    or die "Error getting csv data. ($!)";  #this is where it dies on a dos formatted file

When I want to be sure that line terminators are being totally
stripped on Windows, I do

     chomp;
     s/\r+$//;

That gets rid of the Windows standard \r\n line-terminator.  I had a
problem for a little while where some lines were ending in \r\r\n or
\r\r\r\n (some source-control parameters were misset), so the above
code would handle them too.  If you want to stomp out \r even in the
middle of lines, where they shouldn't be anyway, I think it's

     tr/\r//d;

but I've not tested it and I very rarely use tr///.

-- 
Tim McDaniel, tmcd at panix.com


More information about the Austin mailing list