Link to home
Start Free TrialLog in
Avatar of ka1a
ka1a

asked on

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.
Avatar of sburck
sburck

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);
ASKER CERTIFIED SOLUTION
Avatar of Fatman121898
Fatman121898

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Of course, you can set buffersize as you need (but <=64k).
:-)
Jo.