且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何检测文件中的行尾?

更新时间:2023-09-18 23:06:40

你可以做到。 ..但这不一定是个好主意。正如CPallini所说,如果你在二进制文件中寻找一个行结尾 - 它没有行,只有八位二进制值 - 那么你就会遇到问题。即使文件包含文本,存储在文件中以指示行尾的实际数据也可能因系统而异:\ n,\ r和\ n \ 很常见。因此,检查自己会变得复杂。



相反,为什么不将数据视为它所写的格式?假设它是文本(或换行是你问题中最少的),只需使用:

You can do it...but it's not necessarily a good idea. As CPallini says, if you are looking for a line end in a binary file - which don't have lines, just eight bit binary values - then you will have problems. And even if the file contains text, the actual data stored in the file to indicate the "end of a line" can differ from system to system: "\n", "\r", and "\n\r" are pretty common. So checking yourself can get complicated.

Instead, why not treat the data as the format it was written? Assuming it's text (or newlines are the least of your problems) just use:
string[] lines = File.ReadAllLines(path);



或者只是将ReadLine与StreamReader一起使用: https://msdn.microsoft.com/en-us/library/aa287535(v = vs.71).aspx [ ^ ]

让系统对其进行排序。


Or just use ReadLine with your StreamReader: https://msdn.microsoft.com/en-us/library/aa287535(v=vs.71).aspx[^]
And let the system sort it out.