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.
精彩评论