RT

解决方案 »

  1.   

    该函数会把指定文件中字串stxt替换为toTxt
    procedure restr(fn: Ansistring; stxt, toTxt: string);
    var 
     StrL, x, pos, fpos, num, readnum: integer;
     buf: array[0..500] of char;
     SFile: file;
     L, H: char;
    begin
     StrL := Length(stxt);
     AssignFile(sfile, fn);
     reset(sfile, 1);
     repeat
      buf := #0;
      Num := 0;
      fpos := filepos(sfile);
      blockread(sfile, buf, Sizeof(Buf), readnum);
      repeat
       Pos := num;
       for x := 1 to StrL do begin
        L := sTxt[x]; H := Buf[num];
        if L = H then begin
         inc(num);
         if x = StrL then begin
          seek(sfile, fpos + pos);
          blockWrite(sfile, toTxt[1], Length(totxt));
         end;
        end
        else begin
         if x = 1 then inc(num);
         break;
        end;
       end;
      until num >= high(buf);
     until readnum <= 0;
     closefile(sfile);
    end;procedure TForm1.Button1Click(Sender: TObject);
    begin
     Findstr('文件名', '003e2b6c3d', '53e2e6c3fs');
    end;  
      

  2.   

    //替换指定串
    FILE fp1;
    unsigned char *ch=new char[FILELENGTH];
    int i,len;
    fp1=fopen("filename","rb+");
    len=fread(ch,1,FILELENGTH,fp1);
    for (i=0;i<len-4;i++) {
      if (!memcmp(ch,"\x0\x3e\x2b\x6c\x3d",5)) break;
    }
    if (i<len-4) memcpy(ch+i,"\x5\x3e\x2e\x6c\x3f",5);
    fseek(fp1,0,SEEK_SET);
    fwrite(ch,1,len,fp1);
    fclose(fp1);