Question

im having a little trouble with a binary writer issue, my program is set to load SHIFT-JIS characters, and write in the same encoding, but it returns a file bigger in size :s although i remove several Characters, here is a sample of the files before and after writing, these are the open and save codes:

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;
    }

i truly appreciate your help.

Was it helpful?

Solution

Looking inside the "After" file in the Samples.zip you provided seems to indicate that you're outputting the same string twice, or something...

I would suggest you start with smaller files to test on, and to use the debugger to step in.

Actually, having tried that myself, i think i found out what's going on: you're using "nextOffset - currentOffset" as a stringLength, but it's actually a BYTE-Length... The char array will end (have a '\0' inside it) after only half this number, and when outputting you'll also get the second half, that you haven't yet read.

[Edit] Posting the code from my comment below, for better formatting:

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;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top