What is a reliable way to detect the line feed characters used in a file?

advertisements

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?


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.