如何在二进制文件中找到一个特定的字符串

解决方案 »

  1.   

    pos函数可以
    把你要找的文件用文件映射读出来
    然后把你的字符串用pos查找
    =======================================================================
    {========================================================================
            文件:   Project        功能:
            ------------------------------------------------------------
                    在文件中查询某个字符串
            ------------------------------------------------------------        说明:
            ------------------------------------------------------------
                    由于查询文件非常慢,但是使用文件映射可以
                    提高速度,本测试程序就是演示如何使用文件
                    映射查询字符串。
                    原理:
                            先打开文件得到文件句柄
                            然后使用文件句柄创建文件
                            映射,然后用文件映射创建
                            文件视图,然后查询。
            ------------------------------------------------------------        作者: 周斯洋
            时间: 2002/09/12 08:15:00
            地点: 齐齐哈尔
            版本: 1.0        警告:
            ------------------------------------------------------------        ------------------------------------------------------------
    ========================================================================}
    //=======================================================================
    //
    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      TForm1 = class(TForm)
        Button1: TButton;
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}
    {-------------------------------------------------------------------------
      Function:
              查询字符串第times出现的位置
      Parameters:
              psource:  源字符的地址
              pfindstr: 需要查询字符的地址
              times: 出现的次数
      Return Values
              成功: 返回times出现的位置
              失败: 返回nil
      Res
     -------------------------------------------------------------------------}
    function FindStr(psource: pchar; pfindstr: pchar; times: integer): pchar;
    var
      pData: Pchar;
    begin
      if times > 0 then
      begin
        pData := psource;
        while times >0 do
        begin
          pData := StrPos(pData,pfindstr);
          if pData = nil then
          begin
            result := pData;
          end;
          inc(pData);
          dec(times);
        end;
        result := pData - 1;
      end
      else begin
        result := nil;
      end;end;
    procedure TForm1.Button1Click(Sender: TObject);
    var
      hFile: Cardinal;
      fSize: integer;
      fData: pByte;
      pData: pchar;
      hMap:  THandle;
    begin
      //
      //打开文件得到文件句柄
      //
      hFile := FileOpen('Test.TXT',fmOpenReadWrite);
      if hFile = 0 then
      begin
        showmessage('File Not Exist or File Was Be Broken');
        exit;
      end;
      try
        //
        //得到文件大小,创建文件映射
        //
        fSize := GetFileSize(hFile,0);
        hMap := CreateFileMapping(hFile, nil, PAGE_READWRITE, 0, fSize,nil);
        if hMap = 0 then
        begin
          showmessage('CreateFileMapping Error');
        end;
        //
        //使用文件映射创建文件视图
        //
        fData := MapViewOfFile(hMap, File_MAP_ALL_ACCESS,0,0,fSize);
        if fData = nil then
        begin
          showmessage('MapViewOfFile Error');
        end;
      finally
        Closehandle(hMap);
      end;
      try
        //
        //利用视图进行查询
        //
        showmessage(findstr(pchar(fdata),'a',1));
      finally
        UnmapViewOfFile(fData);
      end;
    end;
    end.
      

  2.   

    先把文件内容读入一个二进制数组中,再用Ord分别把你要查找的字符串转化为其对应的AscII码,然后遍历这个二进制数组就可以找到了