-1

我想从文件中读取一个字节块(例如 400KB ),替换缓冲区中的一些文本,然后将其写入文件。最初我尝试使用 TFileStream 缓冲区字节数组,但后来我遇到了 stringreplace 与字符串一起使用的问题。源数据为 txt UTF-8。这就是我所拥有的:

var
  SS,ST: TFileStream;
  Buffer: string;
  sf,tf,TempStr: string;
  i: Integer;
begin
  sf := 'U:\SYSTEM\enwiktionary-latest-stub-articles\stub-articles.xml';
  tf := 'A:1.txt';
  SS := TFileStream.Create(sf, fmOpenRead);
  ST := TFileStream.Create(tf, fmCreate or fmOpenWrite);
  try
    SS.Read(Buffer, sizeof(Buffer));
    Buffer := stringreplace(Buffer, '<page>','<p>', [rfReplaceAll]);
    Buffer := stringreplace(Buffer, '</page>','</p>', [rfReplaceAll]);
    Buffer := stringreplace(Buffer, '<title>','<t>', [rfReplaceAll]);
    Buffer := stringreplace(Buffer, '</title>','</t>', [rfReplaceAll]);
    Buffer := stringreplace(Buffer, '<ns>','<n', [rfReplaceAll]);
    Buffer := stringreplace(Buffer, '</ns>','>', [rfReplaceAll]);
    Buffer := stringreplace(Buffer, '<revision>','<r>', [rfReplaceAll]);
    Buffer := stringreplace(Buffer, '</revision>','</r>', [rfReplaceAll]);
    Buffer := stringreplace(Buffer, '<id>','<i', [rfReplaceAll]);
    Buffer := stringreplace(Buffer, '</id>','>', [rfReplaceAll]);
    Buffer := stringreplace(Buffer, '<parentid>','<pi', [rfReplaceAll]);
    Buffer := stringreplace(Buffer, '</parentid>','>', [rfReplaceAll]);
    Buffer := stringreplace(Buffer, '<contributor>','', [rfReplaceAll]);
    Buffer := stringreplace(Buffer, '</contributor>','', [rfReplaceAll]);
    Buffer := stringreplace(Buffer, '<username>','<u>', [rfReplaceAll]);
    Buffer := stringreplace(Buffer, '</username>','</u>', [rfReplaceAll]);
    Buffer := stringreplace(Buffer, '<comment>','<c>', [rfReplaceAll]);
    Buffer := stringreplace(Buffer, '</comment>','</c>', [rfReplaceAll]);
    Buffer := stringreplace(Buffer, '<text id="','<t =', [rfReplaceAll]);
    Buffer := stringreplace(Buffer, '" bytes="',' b=', [rfReplaceAll]);
    ST.Write(Buffer, sizeof(Buffer));
  finally
    SS.Free;
  end;

Buffer := stringreplace使运行时错误访问冲突..

4

2 回答 2

2

使用 UTF8 缓冲区字符串并为字符串主体分配位置:

 Buffer: AnsiString; //type UTF8String = AnsiString;
 ...
 SetLength(Buffer, BlockSize)
 SS.Read(PAnsiChar(Buffer)^, BlockSize);
 ...
 ST.Write(PAnsiChar(Buffer)^, Length(Buffer)); 

但是使用这种方法,您可以在块的边界处松散图案。为什么不使用TStringList,加载其中的所有内容并使用它的行?

于 2018-06-30T12:55:00.533 回答
0

一个工作示例。

var
    ifname,ofname:string;
    sourceStream, targetStream: TFileStream;
    filesizevalue, size:integer;
    Buffer: AnsiString;
begin
  ifname := 'U:\SYSTEM\enwiktionary-latest-stub-articles\stub-articles.xml';
  ofname := 'A:1.txt';
  filesizevalue:=900000;
  size := 1;
  SetLength(Buffer, filesizevalue);
  sourceStream := TFileStream.Create(ifname, fmOpenRead);
  targetStream := TFileStream.Create(ofname, fmCreate or fmOpenWrite);
  try
    sourceStream.seek(0, soFromBeginning);
    sourceStream.ReadBuffer(PAnsiChar(Buffer)^, filesizevalue * size);

    buffer := stringreplace(buffer, '<page>','<p>', [rfReplaceAll]);
    buffer := stringreplace(buffer, '</page>','</p>', [rfReplaceAll]);
    buffer := stringreplace(buffer, '<title>','<t>', [rfReplaceAll]);
    buffer := stringreplace(buffer, '</title>','</t>', [rfReplaceAll]);
    buffer := stringreplace(buffer, '<ns>','<n', [rfReplaceAll]);
    buffer := stringreplace(buffer, '</ns>','>', [rfReplaceAll]);
    buffer := stringreplace(buffer, '<revision>','<r>', [rfReplaceAll]);
    buffer := stringreplace(buffer, '</revision>','</r>', [rfReplaceAll]);
    buffer := stringreplace(buffer, '<id>','<i', [rfReplaceAll]);
    buffer := stringreplace(buffer, '</id>','>', [rfReplaceAll]);
    buffer := stringreplace(buffer, '<parentid>','<pi', [rfReplaceAll]);
    buffer := stringreplace(buffer, '</parentid>','>', [rfReplaceAll]);
    buffer := stringreplace(buffer, '<contributor>','', [rfReplaceAll]);
    buffer := stringreplace(buffer, '</contributor>','', [rfReplaceAll]);
    buffer := stringreplace(buffer, '<username>','<u>', [rfReplaceAll]);
    buffer := stringreplace(buffer, '</username>','</u>', [rfReplaceAll]);
    buffer := stringreplace(buffer, '<comment>','<c>', [rfReplaceAll]);
    buffer := stringreplace(buffer, '</comment>','</c>', [rfReplaceAll]);
    buffer := stringreplace(buffer, '<text id="','<t =', [rfReplaceAll]);
    buffer := stringreplace(buffer, '" bytes="',' b=', [rfReplaceAll]);
    targetStream.Write(PAnsiChar(buffer)^, length(buffer) );
  finally
     sourceStream.free;
     targetStream.free;
  end;
于 2018-06-30T21:14:38.837 回答