Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Difference between using \r\n and \n on Linux and Windows machines

by kamesh3183 (Beadle)
on Jun 09, 2005 at 06:48 UTC ( [id://464982]=perlquestion: print w/replies, xml ) Need Help??

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

Hi, I am wondering whether output generated by the following scripts are same on Linux and Windows machines.
use strict; use warnings; my $output = "output.txt"; die "can't open output file: $!" unless open FH, ">$output"; printf(FH "%s\n", "there won't be any junk characters in this version" +); close FH;
use strict; use warnings; my $output = "output2.txt"; die "can't open output file: $!" unless open FH, ">$output"; printf(FH "%s\r\n", "I wonder any junk charcter will be printed to the + file on windows"); close FH;
Can you please suggest which one is more portable?

Replies are listed 'Best First'.
Re: Difference between using \r\n and \n on Linux and Windows machines
by bart (Canon) on Jun 09, 2005 at 06:55 UTC
    Use the same version as on Linux, on Windows. The output layer for the file handle FH will, when in text mode (i.e. when binmode hasn't been used on it, like here) convert every "\n" character to the CR+LF sequence — or, like you typed, "\r\n", at least on Windows and Linux, as this notation isn't very portable.

    So, if you insert a "\r" character yourself, you will end up with two because perl inserts one itself next to the "\n". So don't do that.

    p.s. If you want to create a Unix text file on Windows, you can do

    binmode FH;
    before you print anything to it, and this conversion is prevented, you'll get an identical result on Windows as on Linux.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://464982]
Approved by ZlR
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (2)
As of 2024-04-19 20:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found