开发者

What is a reliable way to detect which line break characters are used in a file?

I'm reading text files but I don't kno开发者_如何转开发w in advance the kind of line ending used in this file. I need to know whether it uses UNIX \n or windows style \r\n line endings.

What is a reliable and fast way to detect this?


Maybe I'm missing a trick, but couldn't you just open the file as binary and read bytes until you see '\n'? If you saw '\r' just before, it's Windows. Otherwise it's Unix.


From wikipedia:

The file command also detects the type of EOL used:

file myfile.txt
> > myfile.txt: ASCII text, with CRLF line terminators

Source: http://en.wikipedia.org/wiki/Newline#Common_problems

Of course, file would only work on Unix systems. I'm sure someone else will answer one for Windows.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜