Delphi读取特定地址的文件 [英] Delphi read file at specific address

查看:87
本文介绍了Delphi读取特定地址的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题非常基本,因为我处于非常基本的水平。我必须读取扩展名为 *。pam 的文件的内容。这并不常见,它是由特定程序生成的文件,其固定大小为xx KB。其中有一些数字和值。

This question is very basic because I am at a very basic level. I have to read the contents of a file with a *.pam extension. It's not common, that's a file generated by a specific program and it has a fixed size of xx KB. There are some numbers and values inside it.

我需要读取此文件,因此我将使用 TStreamReader 。当然,我需要知道文件的结构,但是我不需要。顺便说一下,我上面的图片是包含有用信息的表的一部分:

I need to read this file and so I am going to use TStreamReader. Of course I need to know the structure of the file but I don't. By the way I have the picture above that is a part of a table with useful info:

含义如下:打开文件后,转到特定地址并获取数据。例如,在0x30中,我可以找到有关移动1当前PP的数据。我的问题是:如何跳转到给定的地址?

The meaning is the following: once you've opened the file, go to a specific address and get the data. For example, in 0x30 I can find the data about 'Move 1 Current PP'. My question is: how do I jump to a given address?

procedure TForm1.Button1Click(Sender: TObject);
var sw: TStreamReader;
begin

 Memo1.Clear;    
 sw := TStreamReader.Create('C:\aaa\myfile.pam', TEncoding.UTF8);
 try

  sw.Position := 0;

 finally
  sw.Free;
 end;

我在文档中看到有类似 Seek 位置,但看来我无法使用它们。通常,我会使用

I have seen in the documentation that there is something like Seek and Position but it seems that I cannot use them. In general I would use

while not(sw.EndOfStream) do
 begin
  //read...
 end;

这是从头开始的,它顺序读取数据。如何跳转到特定地址并阅读内容?我想我可以将十六进制地址传递给类似Seek的函数,这样我就可以转到特定点了。这样我就可以检索数据了。

This starts from the beginning and it reads the data in seqence. How can I jump to a specific address and read the content? I guess that I can pass the hex address to a function like Seek, so I can go to a specific point. Then I can just retrieve the data.

推荐答案

如果文件内容是二进制文件(与文本文件相反),则不要使用任何形式文本编码选项。

If the file content is binary (as opposite to textual) do not use any kind of text encoding options.

在下面的示例中,我使用 TFileStream 处理文件并读取其中的选定部分文件。 过程ReadBuffer()方法具有重载,该重载将使用无类型的缓冲区或类型为 TBytes 的缓冲区。如果无法读取请求的字节数,则会引发 EReadError 异常。

In the following example I use TFileStream to handle the file and to read selected parts of the file. The procedure ReadBuffer() method has overloads that takes either an untyped buffer or a buffer of type TBytes. It raises an EReadError exception if requested number of bytes can not be read.

var
  fn: TFileName;
  fs: TFileStream;
  b: byte;
  bf: array[0..3] of word;
  ReadResult: integer;
begin
  fn := 'c:\tmp\MyMagicalFile.pam';
  fs := TFileStream.Create(fn, fmOpenRead or fmShareDenyWrite);
  try
    fs.Seek($28, soFromBeginning);    // move to offset $28 in the file
    fs.ReadBuffer(bf, 8);             // read 4 x 2 bytes into buffer (bf)
                                      // fs.Position is at this point at $30
    fs.ReadBuffer(b, 1);              // read one byte (at offset $30) into the buffer (b)
  finally
    fs.free;
  end;
  // use b and bf as needed
end;

如上面的代码,您可以使用<$ c跳转到文件中的给定位置$ c> Seek()方法。读取(或写入)时,位置会自动更新。 SeekOptions soFromBeginning soFromCurrent soFromEnd
有关其他详细信息,请参见 System.Classes.TFileStream

As in the code above, you jump to a given position in the file by using the Seek() method. The position is automatically updated when reading (or writing). SeekOptions are soFromBeginning, soFromCurrent and soFromEnd. For other details see System.Classes.TFileStream.

这篇关于Delphi读取特定地址的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆