My First Post      My Facebook Profile      My MeOnShow Profile      W3LC Facebook Page      Learners Consortium Group      Job Portal      Shopping @Yeyhi.com









Friday, July 10, 2020

Line ending difference - Windows vs Linux Mac : Line Breaks \n CRLF explained and resolutions in Python, Java, Javascript, C++, Node.JS etc


The Windows environment and the UNIX environment use different end of line characters.  So, if you are sharing files between the environments, one of the environments sees end of line characters at the end of each line of text that it does not normally expect, while the other may give error.

In general, z/OS UNIX text files contain a newline character at the end of each line. In ASCII, newline is X'0A'. In EBCDIC, newline is X'15'. (For example, ASCII code page ISO8859-1 and EBCDIC code page IBM-1047 translate back and forth between these characters.) Windows programs normally use a carriage return followed by a line feed character at the end of each line of a text file. In ASCII, carriage return/line feed is X'0D'/X'0A'. In EBCDIC, carriage return/line feed is X'0D'/X'15'.



As per my research, when you give a Line break there are different behaviours or interpretation based on the platform you are using:


  • Windows: '\r\n'
  • Mac (OS 9-): '\r'
  • Mac (OS 10+): '\n'
  • Unix/Linux: '\n'


Resolution:

Identify in the program which operating system you are using. From there find the Line separator value. Following are the methods you can use for some different programming languages you would use:


Python:

import os
os.linesep

Java:

System.lineSeparator()

NodeJS:

require('os').EOL

C++:

Many methods including Boost Library
One method QDir::separator().toAscii()

C#

string eol = Environment.NewLine;


Cheers :)

No comments:

Post a Comment