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?
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;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With