cleanup a ASCII file

Hello everyone,

I need to cleanup a ASCII file.  With cleanup I mean remove certain characters (#13, #10, ...).  At this moment I open the file as ‘file of bytes’.  I create a another ‘file of bytes’.  I read a character.  If the character is in the list I give up, the character is written to the new file, else it will not be written.  And so on to the end of the file.  It works, but it is very slow (most files are around 2 Mb).

Is there another (faster) way to do this.  The cleanup must be correct.  So the procedure must be 100 % reliable.

All suggestions are welcome.

Thanks in advance.

Dirk.

PS sorry for my poor english.
ka1aAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sburckCommented:
Using BlockRead and Blockwrite, you can move large chunks of the file to memory and do the cleanup there.  Something like (short paraphrase):

BufferR,BufferW : array[0..$7FFF] of byte;
OldFile,NewFile : File;
.....
AssignFile(OldFile,'oldfile');
Reset(OldFile,1);
AssignFile(NewFile,'newfile');
Rewrite(NewFile,1);
repeat
     BlockRead(OldFile,BufferR,$7FFF,amtread);
     if amtread > 0 then
     begin
          amttowrite := 0;
          for i:=0 to amtread do
             if not(BufferR[i] in [#10,#13..whatever]) then
          begin
               BufferW[amttowrite] := BufferR[i];
               inc(amttowrite);
          end;
          Blockrite(NewFile,BufferW,amttowrite);
     end;
until amtread = 0;
CloseFile(OldFile);
CloseFile(NewFIle);
0
Fatman121898Commented:
Hi ka1a,

You should use well-known procedures BlockRead and BlockWrite
as follows:


procedure CleanupFile(FromFileName,ToFileName:String;CharToRemove:Char);
var
  FromF, ToF: file;
  NumRead, NumWritten: Integer;
  Buf1, Buf2: array[1..2048] of Char;
  I: Cardinal;
  begin
    AssignFile(FromF, FromFileName);
    Reset(FromF, 1);      { Record size = 1 }
    AssignFile(ToF, ToFileName);      
    Rewrite(ToF, 1);      { Record size = 1 }
    repeat
      BlockRead(FromF, Buf1, SizeOf(Buf1), NumRead);
      I:=1;
      J:=1;
      //here characher is removed
      for I:=1 to NumRead do
        if Buf1[I]<>CharToRemove
          then
            begin
              Buf2[J]:=Buf1[I];
              Inc(J);
            end
      BlockWrite(ToF, Buf, J-1, NumWritten);
    until (NumRead = 0) or (NumWritten <> (J-1));
    CloseFile(FromF);
    CloseFile(ToF);
end;


Regards.
Jo.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Fatman121898Commented:
Of course, you can set buffersize as you need (but <=64k).
:-)
Jo.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.