22

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?

Daisetsu
  • 4,846
  • 11
  • 50
  • 70
  • Are you seeing the ^M char when you open the file? Just curious. I remember reading something about this in "Classic Shell Scripting" I think. I'm curious as to what the answer will be. Sorry not much help. :-) I would expect that you have to write a shell script that will clean your files of this character before you open them on the mac. – mledbetter Sep 15 '10 at 18:33
  • What is the destination on the Mac? ie, what is going to open the file that you are creating? – dawg Sep 15 '10 at 18:41
  • @Mledbetter, if you see that, it usually means three things are true: First, you're viewing a Windows or old Mac text file. Second, your editor is in Linux or new Mac mode. And third, your editor displays control characters with circumflexes followed by a non-control character. – Rob Kennedy Sep 15 '10 at 20:06
  • 2
    According to Deep Thought, the answer is "42" – mjn Sep 16 '10 at 13:48
  • @mjustin, aah...the * character. It all adds up. – riwalk Sep 16 '10 at 16:05

6 Answers6

44
  • 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.

riwalk
  • 14,033
  • 6
  • 51
  • 68
  • 5
    The only "universal" newline fact is that there is no such thing as LFCR. This fact can be abused to be able to process files from all 3 operating systems: first check if it's LF otherwise if it's CR check if the next character is LF. – slebetman Sep 15 '10 at 18:49
  • 1
    Nearly true - but there are systems that don't use any combination of CR or LF for line ends. Certainly there are systems that use EBCDIC and other non-ASCII-related character sets. There are even some systems, I believe (perhaps someone can confirm?), that use ASCII but use a more sophisticated data structure for text files than a simple sequence of character codes, so there are no line-end characters at all. –  Sep 15 '10 at 19:44
  • 3
    +1 for mentioning there is no universal way; you should have mentioned http://en.wikipedia.org/wiki/Newline as there are more than the 4 examples than you mentioned – Jeroen Wiert Pluimers Sep 16 '10 at 12:03
  • Watch out for older versions of Excel on Mac (possibly even current ones - but I am using Office 2011), as I believe that it uses Windows line-endings, rather than OS X, which is a little confusing if you try to process those files via a script. – Dunk Feb 22 '18 at 15:41
8

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.

Carl Norum
  • 219,201
  • 40
  • 422
  • 469
5

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.

Toon Krijthe
  • 52,876
  • 38
  • 145
  • 202
  • 2
    Also in the System unit there is a sLineBreak constant that is defined as {$IFDEF LINUX} #10 {$ENDIF} {$IFDEF MSWINDOWS} #13#10 {$ENDIF}. – Alan Clark Sep 15 '10 at 19:34
  • 3
    It's a variable so that you can write a Linux program that processes Windows text files, for example; simply assign a new value to that variable, and everything else works normally. – Rob Kennedy Sep 15 '10 at 20:03
2

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

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

splash
  • 13,037
  • 1
  • 44
  • 67
2

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.

dawg
  • 98,345
  • 23
  • 131
  • 206
1

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");
Griffin
  • 785
  • 5
  • 13