DOS Perl convering LF to CR/LF 
Author Message
 DOS Perl convering LF to CR/LF

I am reading a Unix file on a DOS machines which contains lines ending
in a LF.  The program reads in the line, but when I reprint them to
another file, the LF is now a CR/LF!  Is there a way to prevent perl
from this conversion or do I need to cop off the end and print ending
with a LF?

Also, what is the control char(s) for LF only, eg. like the "\n" for
CRLF?

Thanks



Thu, 23 Oct 2003 07:03:18 GMT  
 DOS Perl convering LF to CR/LF
"\n" will translate to LF or CR/LF depending on platform used.
To stop conversion read the file as binary.
G.A.


Quote:
> I am reading a Unix file on a DOS machines which contains lines ending
> in a LF.  The program reads in the line, but when I reprint them to
> another file, the LF is now a CR/LF!  Is there a way to prevent perl
> from this conversion or do I need to cop off the end and print ending
> with a LF?

> Also, what is the control char(s) for LF only, eg. like the "\n" for
> CRLF?

> Thanks



Thu, 23 Oct 2003 09:35:16 GMT  
 DOS Perl convering LF to CR/LF

Quote:

> I am reading a Unix file on a DOS machines which contains lines ending
> in a LF.  The program reads in the line, but when I reprint them to
> another file, the LF is now a CR/LF!

That's because you're writing the file in text mode, which automatically
translates LF to CRLF.

Quote:
>  Is there a way to prevent perl from this conversion

Yes. Use binary mode (binmode HANDLE where HANDLE is the name of your output
filehandle).

Quote:
> Also, what is the control char(s) for LF only, eg. like the "\n" for
> CRLF?

Wrong question. "\n", on most machines (not the Mac, for example) stands for
plain LF. However, on DOSish machines, LF gets autotranslated to CRLF on output,
and CRLF gets autotranslated to LF on input.

So if you read a file containing CRLFs, they'll appears as simple LFs ("\n") to
the program; if you write out that data, they'll become CRLFs again.

Use binmode() to prevent LF turning into CRLF on output, or to keep CRLF as CRLF
on input. (On such platforms, reading a file in text mode will probably also
cause you to get EOF at the first occurrence of "\cZ" or "\x1a", so if your
input contains such characters as part of normal data, you will probably also
need binmode() on your input filehandle.)

Cheers,
Philip
--

That really is my address; no need to remove anything to reply.
If you're not part of the solution, you're part of the precipitate.



Thu, 23 Oct 2003 15:50:55 GMT  
 DOS Perl convering LF to CR/LF

Quote:

>I am reading a Unix file on a DOS machines which contains lines ending
>in a LF.  The program reads in the line, but when I reprint them to
>another file, the LF is now a CR/LF!  Is there a way to prevent perl
>from this conversion or do I need to cop off the end and print ending
>with a LF?

binmode() on the output handle.

Quote:
>Also, what is the control char(s) for LF only, eg. like the "\n" for
>CRLF?

As you noticed, "\n" is chr(10), just as in Unix. So it's just one
character, not two. Conversion from CRLF to "\n" happens on input, and
from "\n" to CRLF happens on output. binmode() prevents this conversion.

--
        Bart.



Thu, 23 Oct 2003 19:34:54 GMT  
 DOS Perl convering LF to CR/LF


Quote:
>As you noticed, "\n" is chr(10), just as in Unix. So it's just one
>character, not two. Conversion from CRLF to "\n" happens on input, and

\n is chr(13) on Macs. You can generalize that to \n is always the
end of line character on text files.


Fri, 31 Oct 2003 09:47:37 GMT  
 DOS Perl convering LF to CR/LF


Quote:


> >As you noticed, "\n" is chr(10), just as in Unix. So it's just one
> >character, not two. Conversion from CRLF to "\n" happens on input, and

> \n is chr(13) on Macs. You can generalize that to \n is always the
> end of line character on text files.

More like (I think): 'if you read in a text file, lines will appear to
be terminated by "\n".'

On disk, they could be terminated "\015" (Mac), "\015\012" (DOS), "\012"
(Unix) -- or even unterminated but father fixed-length and space-padded
(some mainframes?). But I think that even in the latter case, Perl's I/O
abstracts that away and gives you the illusion that lines are terminated
by "\n"[1], regardless of whether "\n" eq "\015" or "\n" eq "\012" or
"\n" eq "{whatever it is in EBCDIC}".

Cheers,
Philip

[1] Though this would mean that you couldn't write spaces at the end of
a line and expect them still to be there when you read the file in
again.
--

That really is my address; no need to remove anything to reply.
If you're not part of the solution, you're part of the precipitate.



Fri, 31 Oct 2003 13:22:43 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Converting LF to CR+LF

2. Any Utility in Win32/Win95/Dos to remove CR or LF

3. Question about formatting/(CR/LF) in PERL

4. Perl and CR+LF

5. Cr Lf and Perl for Win NT

6. NT Perl and CR/LF

7. Missing LF or getting an Extra LF ?

8. $/ = CR LF?

9. <FILE> and CR-LF

10. Binary data and CR-LF's in MSDOS

11. CR before login or any help that would lf before login

12. CR/LF

 

 
Powered by phpBB® Forum Software