Question

Does your software handle newline characters from other systems?

Linux/BSD    linefeed         ^J    10     x0A
Windows/IBM  return linefeed  ^M^J  13 10  x0D x0A
old Macs     return           ^M    13     x0D
others?

For reasons of insanity, I am going with using the Linux version of the newline character in my text files. But, when I bring my text files over to say Windows, some programs do not play nicely with newline characters in my text. How would you deal with this?

Was it helpful?

Solution

As they say, be strict in what you write and liberal in what you read.

Your application should be able to work properly reading both line endings. If you want to use linefeeds, and potentially upset Windows users, that's fine.

But save for Notepad, most programs I play with seem to be happy with both methods.

(And I use Cygwin on Windows, which just makes everything interesting)

OTHER TIPS

The standard Python distribution comes with two command-line scripts (in Tools/scripts) called crlf.py and lfcr.py that can convert between Windows and Unix/Linux line endings.

[Source]

In .NET, new lines are denoted by Environment.NewLine, so the framework is designed in such a way as to take whatever the system's new line is (CR+LF or CR only or LF only) to use at runtime. Of course this is ultimately useful in Mono.

I suspect you will find that most modern Windows programs (with the notable exception of Notepad) handle newline-only files just fine. However, files generated with windows programs still tend to have crlf endings.

Most of the time, the line endings will automatically be handled in a platform-specific way by the runtime library. For example, a C program that opens a file with fopen(..., "r") will see the lines in a consistent way (linefeed only) on any platform regardless of the actual line endings.

As far as I know, it's only Notepad that has a problem with line separators. Virtually ever other piece of software in the world accepts any of those three types of separator, and possibility others as well. Unfortunately, Notepad is the editor of first resort for most computer users these days. I think it's extremely irresponsible of Microsoft to let this situation continue. I've never played with Vista, but I believe the problem still exists there, as it does in XP. Any body know about the next version?

As others said, there are lot of (quite trivial) converters around, should the need arise. Note that if you do the transfer with FTP in Ascii mode, it will do the conversion automatically...

Indeed, Notepad is the most proeminent program having an issue with LF ending...

The most annoying I saw is text files with mixed line ending, done essentially by people editing a Windows file on Unix, or utilities adding stuff without checking the proper format.

To be happy, just follow recommendation from the standard.

http://unicode.org/standard/reports/tr13/tr13-5.html

And offer options for special cases like old MacOS. Or handle the case automatically if you can detect them reliably.

I recommend formatting your text in Unix style. Forget about Windows users. Because no Windows users use plain-text for document or data. They will be upset if you pass plain-text. They always expect Word or Excel document. Even they use plain-text file, the only problem they will get is just weirdly displaying text.

But Unix users will experience their all tools will work incorrectly. Especially for Unix, follow the standard strictly.

PS. Oh, if your Windows user is a developer, just format with text in Unix, and tell them that's the file from Unix.

Not sure what you mean when you say 'deal' with, but basically you can just say something like:

string convertLineBreaks(String line, String lineBreakYouWant) {
  replace all ^M^J or ^M or ^J in line with lineBreakYouWant

  return line
}

Edit: I suspect after re-reading your question you mean how do you deal with other peoples programs that can't handle incorrect (for the target system) line breaks.

I would suggest either 1) using a program that can deal or 2) running your files through a script that finds line breaks of any type and then converts them into whatever type is right for your system.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top