Posted by Jan Dunn-p12534 on February 27, 2007
I have a short program that reads soem text files then encrypts them. I occasionally get I/O error 6 or 32. Heres my code:unit Unit1; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, Twofish, ClipFn32, StdCtrls; {Twofish is an encryption/decryption component} type TForm1 = class(TForm) Twofish1: TTwofish; procedure FormCreate(Sender: TObject); private { Private declarations } public { Public declarations } end; type TextLines = array[1..5] of string; var Form1: TForm1; implementation {$R *.DFM} procedure TForm1.FormCreate(Sender: TObject); var sr:TSearchRec; done:string; fName:string; InFile:textfile; LineofText:TextLines; i:integer; DotheEncryption:Boolean; begin DotheEncryption:=False; Twofish1.InitialiseString('JanDunn'); If FindFirst('*.doc',faAnyFile,sr)=0 then { Let's load the file and test for whether it contains an unencrypted first line} fname:=sr.name; Assignfile(InFile,fname); Reset(Infile); { read the first 5 lines of text to look for *****; each unencrypted file starts with **** in one of the first 5 lines- this test for whether the file is already encrypted } for i:=1 to 5 do begin Readln(Infile,LineofText[i]); end; CloseFile(Infile); { Now see if any lines have **** in it } i:=1; For i:= 1 to 5 do begin If Copy(LineofText[i],1,4)='****' then DotheEncryption:=True; end; If DotheEncryption=True then begin TwoFish1.EncFile(sr.name,'temp'); FileShred(sr.Name); RenameFile('temp',sr.Name); end; While done <> 'done' do begin If FindNext(sr)=0 then begin { Let's load the file and test for whether it contains an unencrypted forst line } fname:=sr.name; Assignfile(InFile,fname); Reset(Infile); { read the first 5 lines of text to look for ***** } for i:=1 to 5 do begin Readln(Infile,LineofText[i]); end; CloseFile(Infile); { Now see if any lines have **** in it } i:=1; For i:= 1 to 5 do begin If Copy(LineofText[i],1,4)='****' then DotheEncryption:=True; end; If DotheEncryption=True then begin TwoFish1.EncFile(sr.name,'temp'); FileShred(sr.name); RenameFile('temp',sr.Name); end; end else done:='done'; end; FindClose(sr); done:=''; { now let's do the txt files } If FindFirst('*.txt',faAnyFile,sr)=0 then { Let's load the file and test for whether it contains an unencrypted first line } fname:=sr.name; Assignfile(InFile,fname); Reset(Infile); { read the first 5 lines of text to look for ***** } for i:=1 to 5 do begin Readln(Infile,LineofText[i]); end; CloseFile(Infile); { Now see if any lines have **** in it } i:=1; For i:= 1 to 5 do begin If Copy(LineofText[i],1,4)='****' then DotheEncryption:=True; end; If DotheEncryption=True then begin TwoFish1.EncFile(sr.name,'temp'); FileShred(sr.Name); RenameFile('temp',sr.Name); end; While done <> 'done' do begin If FindNext(sr)=0 then begin { Let's load the file and test for whether it contains an unencrypted forst line } fname:=sr.name; Assignfile(InFile,fname); Reset(Infile); { read the first 5 lines of text to look for ***** } for i:=1 to 5 do begin Readln(Infile,LineofText[i]); end; CloseFile(Infile); {Now see if any lines have **** in it} i:=1; For i:= 1 to 5 do begin If Copy(LineofText[i],1,4)='****' then DotheEncryption:=True; end; If DotheEncryption=True then begin TwoFish1.EncFile(sr.name,'temp'); FileShred(sr.name); RenameFile('temp',sr.Name); end; end else done:='done'; end; FindClose(sr); Application.terminate; end; end.Related articles