首页 > 解决方案 > Delphi-如何在保存之前删除非 ANSI(不可打印)字符?

问题描述

有人可以指导我以某种方式扩展此过程,以便在将流保存到文件之前删除所有不可打印字符或替换为 SPACE 吗?字符串从二进制中读取,最大大小为 1 MB。我的程序:

var
  i : Word;
  FileName : TFileName;
  SizeofFiles,posi : Integer;
  fs, sStream: TFileStream;
  SplitFileName: String;
begin
  ProgressBar1.Position := 0;
  FileName:= lblFilePath.Caption;
  SizeofFiles := StrToInt(edt2.Text)  ;
  posi := StrToInt(edt1.text) ;
  fs := TFileStream.Create(FileName, fmOpenRead or fmShareDenyWrite);
  try
             fs.Position := Posi ;
    begin
      SplitFileName := ChangeFileExt(FileName, '.'+ FormatFloat('000', i));
      sStream := TFileStream.Create(SplitFileName, fmCreate or fmShareExclusive);
      try
        if fs.Size - fs.Position < SizeofFiles then
          SizeofFiles := fs.Size - fs.Position;
        sStream.CopyFrom(fs, SizeofFiles);
        ProgressBar1.Position := Round((fs.Position / fs.Size) * 100);
      finally
        sStream.Free;
      end;
    end;
  finally
    fs.Free;
  end;
end;

标签: delphistreamsave

解决方案


你将无法再使用TStream.CopyFrom()。您必须Read(Buffer)()从源TStream到本地字节数组,从该数组中删除您不想要的任何内容,然后Write(Buffer)()将剩余字节到目标TStream


推荐阅读