0

我在二进制写入器问题上遇到了一点麻烦,我的程序设置为加载 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;
    }

我真的很感谢你的帮助。

4

1 回答 1

3

查看您提供的 Samples.zip 中的“After”文件似乎表明您正在输出相同的字符串两次,或者什么......

我建议您从较小的文件开始进行测试,并使用调试器介入。

实际上,在我自己尝试过之后,我想我发现了发生了什么:您使用“nextOffset - currentOffset”作为字符串长度,但它实际上是一个字节长度...... char 数组将结束(有一个 '\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;
}
于 2013-04-21T11:38:05.143 回答