[英]Reading bytes from file
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. 我正在尝试编写霍夫曼解压缩,我有一个功能,它试图逐字节读取整个压缩文件,但我有一个问题,它只读取大约150个第一个字节,其余被跳过我不明白为什么。
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. 看起来该函数在文件真正结束之前读取EOF。 I know there is no EOF somewhere between bytes because I'm writing there only byte values.
我知道字节之间没有EOF,因为我只在那里写字节值。
你的while条件是不正确的:它应该读取while (w != -1)
,因此一旦找到零字节就停止循环读取字节,而不是在文件结束时。
while ((w > 0));
当它达到0时,它将停止。将其更改为
while ((w >= 0));
You are probably hitting a zero byte. 你可能正在打一个零字节。
Change your while loop to include 0: 将while循环更改为包含0:
while ((w >= 0));
The problem is in this line: 问题出在这一行:
while ((w > 0)); 而((w> 0));
It should be: 它应该是:
while ((w != -1)); 而((w!= -1));
Because it's likely your file has a 0 in it. 因为你的文件很可能是0。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.