How to make the user not get an Invalid TXT?

2

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?

    
asked by anonymous 09.05.2014 / 14:31

1 answer

3

With And will not really work.

AssignFile(txt, lbCaminho.Caption);
Reset(txt);

if (copy(lTemp, 1, 3) = 'E09')
or (copy(lTemp, 1, 3) = 'E10')
or (copy(lTemp, 1, 3) = 'E11')
or (copy(lTemp, 1, 3) = 'E12')
or (copy(lTemp, 1, 3) = 'E13')
or (copy(lTemp, 1, 3) = 'E14')
or (copy(lTemp, 1, 3) = 'E15')
or (copy(lTemp, 1, 3) = 'E21')
then
begin
  // O codigo acima viria aqui.
end
else
begin
  ShowMessage('Arquivo .TXT inválido.');
end;

CloseFile(txt);

Must be Or , because you need to find at least one of these identifiers, not all in the same position of the same string at the same time.

Switch to this and make sure you are choosing a valid file to test. If the problem persists you may be getting a wrong amount, perhaps because of a wrong position.

Test well, make a debug of the code.

    
09.05.2014 / 14:56