Reputation: 1307
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?
Upvotes: 5
Views: 3582
Reputation: 453
Sounds like the file is actually UTF-16 encoded. Change UTF-8 in your GetString().
Upvotes: 3
Reputation: 2970
Use Encoding.UnicodeEncoding instead. Your file is encoded in UTF-16, not UTF-8.
Upvotes: 8
Reputation: 59705
The file is probably a UTF-16 file, not a UTF-8 file. Just try using Encoding.Unicode
instead of Encoding.UTF8
.
Upvotes: 7