I'm reading text files but I don't know 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?
I'm reading text files but I don't know 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?
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.
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.