What is the universal newline for all operating sy

2020-02-10 17:48发布

When I write a file using Delphi it's on a Windows machine and the text files it puts out work fine on windows. When I use it on a Mac though it's expecting the formatting to be a bit different. On Mac the newline is different and it can't always read the Windows files.

How can I make my files readable by mac programs?

6条回答
Bombasti
2楼-- · 2020-02-10 18:38

There is no universal newline for all operating systems. You have to use linefeed on some, carriage return on others, and both on some others.

Most text editors can handle multiple kinds of line endings - check your documentation. There are also plenty of utilities that can translate line endings for you.

查看更多
【Aperson】
3楼-- · 2020-02-10 18:41

Instead of "universal newline" you could write a "universal format" such as JSON, XML, PDF etc depending if your output is destined to be used as data for another program or a report document to be read by humans.

查看更多
霸刀☆藐视天下
4楼-- · 2020-02-10 18:45

In the system unit there is a global variable DefaultTextLineBreakStyle set based on the OS. It can be tlbsLF or tlbsCRLF. If it is tlbsLF, use #10, if it is tlbsCRLF use #13 #10.

From system:

type
  TTextLineBreakStyle = (tlbsLF, tlbsCRLF);

var   { Text output line break handling.  Default value for all text files }
  DefaultTextLineBreakStyle: TTextLineBreakStyle = 
  {$IFDEF LINUX} tlbsLF {$ENDIF}
  {$IFDEF MSWINDOWS} tlbsCRLF {$ENDIF}
  {$IFDEF MACOS} tlbsLF {$ENDIF};

I just wonder why it's a var and not a const.

查看更多
Juvenile、少年°
5楼-- · 2020-02-10 18:53

Very old thread that is still very relevant. The easiest way to handle this and other situations when you get text data from different operating is to start by normalise the information first. This is Javascript but you should be able to change it into any other language easy enough.

        body = body.replace(/(\r\n|\r)/g,"\n");

In most languages it could be translated into this (but in JS it will just replace the first occurance.)

        body = body.replace("\r\n","\n");
        body = body.replace("\r","\n");

It will simply make sure that newline is represented by "\n" , if you want for instance windows format you simply add after the above..

        body = body.replace("\n","\r\n");
查看更多
Ridiculous、
6楼-- · 2020-02-10 18:54
  • For Windows, it is CRLF
  • For UNIX, it is LF
  • For MAC (up through version 9) it was CR
  • For MAC OS X, it is LF

The simple fact is that it is different for all operating systems. There is no "universal" newline. The best you can do is be aware of the differences.

查看更多
乱世女痞
7楼-- · 2020-02-10 18:54

http://en.wikipedia.org/wiki/Newline

Accordingly you have to write $0D for MacOS up to version 9 and $0A for MacOS X.

查看更多
登录 后发表回答