Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FileStream returning null characters every other character

I seem to be having some issues with a Filestream in C#. I am trying to read the last line from a VERY large text file, 10mb, that is generated by a MSI installer.

The code I am using is:

string path = @"C:\uninstall.log";
byte[] buffer = new byte[100];

using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))
{
    long len = fs.Length;

    fs.Seek(-100, SeekOrigin.End);

    fs.Read(buffer, 0, 100);
}

string foo = Encoding.UTF8.GetString(buffer);
Console.WriteLine("\"" + foo + "\"");

But the output looks similar to this:

H E L L O   W O R L D ! ! ! B L A H   B L A H

Apparently the stream that is read contains a '\0' (null) character every other character. Does anyone know what is causing this?

like image 277
tcables Avatar asked Dec 04 '22 00:12

tcables


1 Answers

Use Encoding.UnicodeEncoding instead. Your file is encoded in UTF-16, not UTF-8.

like image 119
prprcupofcoffee Avatar answered Dec 22 '22 21:12

prprcupofcoffee