I’m trying to write huffman decompress, I have a function which is trying to read byte by byte whole compressed file, but I have a problem it reads only around 150 firsts bytes and rest are skipped I cant understand why.
Here’s the function:
public static StringBuilder odczytBajtowy(string nazwa)
{
FileStream plik = null;
StringBuilder dane = new StringBuilder("");
try
{
plik = new FileStream(@nazwa, FileMode.Open, FileAccess.Read);
int w;
int n = 0;
do
{
n++;
w = plik.ReadByte();
if (w != -1)
dane.Append(StringHelp.Reverse(Convert.ToString((byte)w,2).PadLeft(8, '0')));
}
while ((w > 0));
}
catch (FileNotFoundException)
{
Console.WriteLine("Brak pliku {0}", nazwa);
}
finally
{
if (plik != null)
{
plik.Close();
}
}
return dane;
}
It looks like the function reads EOF before the file is really at the end. I know there is no EOF somewhere between bytes because I’m writing there only byte values.
Your while condition is incorrect : it should read
while (w != -1), so you stop looping through read bytes as soon as a zero byte is found, rather than at the end-of-file.