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;
}
我非常感谢你的帮助。
答案 0 :(得分:3)
查看Samples.zip中的“After”文件,您提供的内容似乎表示您输出的是相同的字符串两次,或者某事......
我建议你先从较小的文件开始测试,然后使用调试器进入。
实际上,自己尝试过后,我想我发现了正在发生的事情:你使用“nextOffset - currentOffset”作为stringLength,但它实际上是一个BYTE-Length ...... 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;
}