views:

273

answers:

5

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?

+7  A: 

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
+1  A: 

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

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

splash
+1 for wikipedia
Jeroen Pluimers
+15  A: 
  • 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.

Stargazer712
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
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.
Steve314
+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 Pluimers
+2  A: 

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.

drewk
+3  A: 

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.

Gamecat
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
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
Sounds reasonable to me.
Gamecat