minixman has asked for the wisdom of the Perl Monks concerning the following question:

All
I have some code in win32 that creates a file
using standard open file write
open(NF,">>$reportlocation\\$accfilename") || die("Unable to open file: $! \n");

and then loops through some arrays and prints each line to the file
printf NF ("$id,$ac,$al,$cr,$cur,$at,$igpl,$risk,$lm,$wm,$ip,");
The problem is when i try and view it on unix, i get the whole
file mixed up and i can't loop through each line and read it
is there a way to create unix friendly files in win32
.

Replies are listed 'Best First'.
Re: Creating Unix Friendly files in win32
by sauoq (Abbot) on Sep 26, 2005 at 08:35 UTC

    You could specify binmode in Windows and carefully leave those carriage returns out...

    Or, you can just clean it up when you get it to your unix box. You might find dos2unix installed on your system. If not, perl -i.bak -pe 's/\r//g' your_file.txt should do it for you.

    -sauoq
    "My two cents aren't worth a dime.";
    
Re: Creating Unix Friendly files in win32
by gargle (Chaplain) on Sep 26, 2005 at 08:31 UTC

    Hi,

    Use a '\n' in your print statement to seperate records/lines.

    When reading the file on unix/linux you'll notice that win32 files tend to end in cr/lf instead of the usual lf. You can easily convert cr/lf files to lf by using the dos2linux command. It will change the file so if you need to go back to the original better make a backup first or use linux2dos.

    Greetings

    --
    if ( 1 ) { $postman->ring() for (1..2); }
Re: Creating Unix Friendly files in win32
by Anonymous Monk on Sep 26, 2005 at 08:58 UTC
    I am not sure what the problem is. As others pointed out, writing (text) files under Windows means logical newlines are written as two byte sequences: a carriage return and a line feed. Unix uses a single line feed as newline.

    But if this was your only problem, it wouldn't prevent you from looping over the file - all what would happen is that your lines end with carriage return/line feed, instead of just a linefeed. Setting $/ appropriately when reading the file, and chomping the lines would fix that.

    Furthermore, the code you give to supposedly writes to the file doesn't even print logical newlines - so there can't be a line ending problem because you don't have newlines!

    Or is not printing newlines the problem, and the Windows vs Unix just a red herring? If you view the file under Windows, is the file then ok? Can you loop through the file under Windows?

    Something else that might happen is that one of the variables you write contains a line-feed - but without the code and the data, that's impossible to judge.

Re: Creating Unix Friendly files in win32
by holli (Abbot) on Sep 26, 2005 at 08:36 UTC
    Apart from what its said above you can create unix files by using the following idiom:
    open OUT, ">>", "test.txt"; binmode OUT; print OUT "some text\x0A"; close OUT;
    \x0A is the linefeed characted in hex notation.


    holli, /regexed monk/
      Just curious: why use \x0A instead of \n? Does the latter get converted even if binmode is set on the filehandle?

      Flavio
      perl -ple'$_=reverse' <<<ti.xittelop@oivalf

      Don't fool yourself.
        Ugh. No. \n and binmode works just fine. But \x0A gets converted to \x0D\x0A if binmode is not present.

        That's a pitfall and I must have mixed it up in my memory.


        holli, /regexed monk/
Re: Creating Unix Friendly files in win32
by randyk (Parson) on Sep 27, 2005 at 06:37 UTC
    If it's a problem with different line endings in a text file between unix and win32, you could open the file using the :crlf layer. For example, this script:
    use strict; use warnings; open(my $fh, '<', 'test.txt') or die $!; my $count = 0; while (<$fh>) { $count++ if /\r/; } close $fh; print "Without :crlf, \$count=$count\n"; open($fh, '<:crlf', 'test.txt') or die $!; $count = 0; while (<$fh>) { $count++ if /\r/; } close $fh; print "With :crlf, \$count=$count\n";
    will print out on a unix machine, for a 10-line file test.txt created on Win32,
    Without :crlf, $count=10 With :crlf, $count=0