c#二进制写入返回更大的文件
本文关键字:文件 返回 二进制 | 更新日期: 2023-09-27 18:10:07
我在二进制写入器问题上遇到了一点麻烦,我的程序设置为加载SHIFT-JIS字符,并以相同的编码编写,但它返回的文件大小更大:s虽然我删除了几个字符,但这里是写入前后文件的示例,这些是打开和保存代码:
private void Openbtn_Click(object sender, EventArgs e)
{
listView1.Items.Clear();
textBox1.Text = "";
menuItem12.Text = "file type is: ";
OpenFileDialog ofd = new OpenFileDialog();
ofd.Title = "Open File";
ofd.Filter = "All Files (*.*)|*.*";
if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
menuItem13.Enabled = true;
menuItem14.Enabled = true;
menuItem15.Enabled = true;
path = ofd.FileName;
BinaryReader br = new BinaryReader(File.OpenRead(path), Encoding.GetEncoding("SHIFT-JIS"));
foreach (char mychar in br.ReadChars(4)) menuItem12.Text += mychar;
if (menuItem12.Text != "file type is: TXTD")
{
MessageBox.Show("This is not a TXTD file...", "Sorry", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
else
{
MessageBox.Show("File opened Succesfully!", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
br.BaseStream.Position = 0x8;
int Pntrnum = br.ReadInt16();
menuItem11.Visible = true;
menuItem11.Text = Pntrnum.ToString();
List<int> offsets = new List<int>();
br.BaseStream.Position = 0x10;
for (int i = 0; i < Pntrnum; i++)
{
offsets.Add(br.ReadInt32());
}
Dictionary<int, string> values = new Dictionary<int, string>();
for (int i = 0; i < offsets.Count; i++)
{
int currentOffset = offsets[i];
int nextOffset = (i + 1) < offsets.Count ? offsets[i + 1] : (int)br.BaseStream.Length;
int stringLength = (nextOffset - currentOffset - 1);
br.BaseStream.Position = currentOffset;
var chars = br.ReadChars(stringLength);
values.Add(currentOffset, new String(chars));
}
foreach (int offset in offsets)
{
listView1.Items.Add(offset.ToString("X")).SubItems.Add(values[offset]);
}
br.Close();
br = null;
}
}
ofd.Dispose();
ofd = null;
}
private void Savebtn_Click(object sender, EventArgs e)
{
BinaryWriter bw = new BinaryWriter(File.OpenWrite(path));
int number_pointers = Convert.ToInt32(menuItem11.Text);
Encoding enc = Encoding.GetEncoding("SHIFT-JIS");
bw.BaseStream.Position = 0x10;
int curr_pointer = 16 + number_pointers * 4;
//pointers writing
for (int i = 0; i < number_pointers; i++)
{
bw.Write(curr_pointer);
curr_pointer += enc.GetByteCount(listView1.Items[i].SubItems[1].Text) + ''0';
}
for (int i = 0; i < number_pointers; i++)
{
bw.Write(enc.GetBytes(listView1.Items[i].SubItems[1].Text + ''0'));
}
bw.Flush();
bw.Close();
bw = null;
}
我真的很感谢你的帮助。
查看您提供的Samples.zip中的"After"文件,似乎表明您输出了两次相同的字符串,或其他内容…
我建议你从较小的文件开始测试,并使用调试器介入。
实际上,我自己试过了,我想我发现了发生了什么:你使用"nextOffset - currentOffset"作为stringLength,但它实际上是一个字节长度…字符数组将在这个数字的一半之后结束(其中有一个''0'),并且在输出时还将得到您尚未读取的另一半。
[编辑]下面是我的评论代码,为了更好的格式:
int index;
for (index = 0; index < stringLength; ++index) if (chars[index] == ''0') break;
if (index < stringLength) {
char[] relevantPart = new char[index];
Array.Copy(chars, relevantPart, index);
chars = relevantPart;
}