views:

947

answers:

7

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?

A: 

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.

Greg Hewgill
A: 

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.

SCdF
Your functions has a problem. Using it to run over a string to detect newlines will detect two for each Windows newline. If you split lines by it, you'll get empty lines.
Eli Bendersky
Fixed it. It's still psuedo code, but the idea is that it greedily consumes potential line breaks and in this case 'fixes' them. That 'fix' is really dependent on what you're trying to do.
SCdF
+3  A: 

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)

Will Hartung
+1  A: 

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]

Ben Hoffstein
+1  A: 

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.

Jon Limjap
And that is the right solution, let the platform take care of the newline.
Gamecat
A: 

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?

Alan Moore
A: 

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.

PhiLho