Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I detect if a file has Unix line feeds (\n) or Windows line feeds (\r\n)?

I am altering a file by means of a FileStream (it is a very large file and I just need to alter the header without rewriting the whole thing.

The file can have either Unix or Windows line feeds, and it is important for me to know which so that I can write the correct line feed characters back into the file when I update it.

I could write a simple function to use a FileStream to read the file in blocks and check for the line feed characters.

But this problem must have been solved before, if not in C# then in the Win32 API?

What's the most efficient way to detect the line feed style of the file?

like image 278
freshr Avatar asked Aug 06 '12 13:08

freshr


1 Answers

Thanks all for your suggestions. I was surprised not to find something easily reusable, so I created a simple function that I include here. Note that it just finds the first newline character (\n or \r\n) and returns that as the match. Enough for my needs, but perhaps not robust.

    public bool TryDetectNewLine(string path, out string newLine)
    {
        using (var fileStream = File.OpenRead(path))
        {
            char prevChar = '\0';

            // Read the first 4000 characters to try and find a newline
            for (int i = 0; i < 4000; i++)
            {
                int b;
                if ((b = fileStream.ReadByte()) == -1) break;

                char curChar = (char)b;

                if (curChar == '\n')
                {
                    newLine = prevChar == '\r' ? "\r\n" : "\n";
                    return true;
                }

                prevChar = curChar;
            }

            // Returning false means could not determine linefeed convention
            newLine = Environment.NewLine;
            return false;
        }
    }
like image 110
freshr Avatar answered Nov 15 '22 14:11

freshr