如何检测文件中的行尾? [英] How to detect end of line in a file?

查看:70
本文介绍了如何检测文件中的行尾?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C#中,使用StreamReader,我如何检测到你到底是什么时候
行。我正在使用Read()函数读取docx文件,我需要

来检测\ n \\\,但我尝试的一切都不起作用。我相信

这可能相当简单,但我无法弄明白。

In C#, using the StreamReader, how do I detect when you get to the end
of line. I am reading a docx file using the Read() function and I need
to detect the \n\r, but everything I try does not work. I am sure that
this probably fairly simple, but I have not been able to figure it out.

推荐答案

你可以做到。 ..但这不一定是个好主意。正如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.


这篇关于如何检测文件中的行尾?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆