Follow the code:
procedure TfrmGrid.btnLoadClick(Sender: TObject);
var
txt: TextFile;
l, treg : integer;
lTemp: String;
begin
treg := 1;
l:= 0;
AssignFile(txt, lbCaminho.Caption);
Reset(txt);
while not eof(txt) do
begin
Readln(txt, lTemp);
if (copy(lTemp, 1, 3)= 'E14') then
begin
inc(treg);
sgCupons.RowCount:=treg;
inc(l);
//COO
sgCupons.Cells[0,l] := copy(lTemp,53 ,6);
//CCF
sgCupons.Cells[1,l] := copy(lTemp,47 ,6);
//S/N
sgCupons.Cells[2,l] := copy(lTemp,4, 20);
//DATA
sgCupons.Cells[3,l] := copy(lTemp,59, 8);
end;
end;
CloseFile(txt);
ShowMessage('Existem '+IntToStr(treg)+' linhas.');
end;
I made this code in order to read the TXT and assign the lines of the sringGrid. But the problem now is that I want to prevent the user from reading an undue file, such as any txt.
So, I thought of this code:
AssignFile(txt, lbCaminho.Caption);
Reset(txt);
if (copy(lTemp, 1, 3)= 'E09')
and (copy(lTemp, 1, 3)= 'E10')
and (copy(lTemp, 1, 3)= 'E11')
and (copy(lTemp, 1, 3)= 'E12')
and (copy(lTemp, 1, 3)= 'E13')
and (copy(lTemp, 1, 3)= 'E14')
and (copy(lTemp, 1, 3)= 'E15')
and (copy(lTemp, 1, 3)= 'E21')
then
begin
// O codigo acima viria aqui.
end
else
ShowMessage('Arquivo .TXT inválido.');
CloseFile(txt);
But it goes into a loop and says that every txt file is invalid, ie the input and output error.
Could anyone help me?