問題描述
換行符 (newline character(s))
您的軟件是否處理來自其他系統的換行符?
Linux/BSD linefeed ^J 10 x0A
Windows/IBM return linefeed ^M^J 13 10 x0D x0A
old Macs return ^M 13 x0D
others?
出於精神錯亂的原因,我將在我的文本文件中使用 Linux 版本的換行符。但是,當我把我的文本文件帶到 Windows 時,有些程序不能很好地處理我的文本中的換行符。你會怎麼處理呢?
參考解法
方法 1:
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)
方法 2:
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.
方法 3:
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.
方法 4:
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.
方法 5:
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?
(by Mark Stock、Will Hartung、Ben Hoffstein、Jon Limjap、Greg Hewgill、Alan Moore)