How to make the user not catch an Invalid TXT?

Asked

Viewed 108 times

2

Follows 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 sringGrid lines. But the problem now is that I want to prevent the user from reading an improper file like 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);

However it enters a loop and says that every txt file is invalid, that is, the input and output error.

Could someone help me?

  • Is it even a and that you want, not a or? (I don’t understand Delphi, but it seems to me that you’re testing an interval for some possible values - and I’d like you to go into if if any of these values are present. I understood right?)

  • 3

    He understood certain same friend, but the problem is that even with the 'and' he is giving problems, and recognizes as 'txt invalido', then I tried now with the 'or', but without success.

  • Exactly Crood! kkkkk, the msm poblema is the while, I can’t put that if inside the while and close the file twice.

1 answer

3


With And won’t work at all.

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);

Needs to be with Or, because you need to find at least one of these identifiers, and not all in the same position as 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 picking up a wrong value, perhaps by a wrong position.

Test well, make a debug of the code.

  • The problem was changing the and for Or msm cara, but had to change the location of the code.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.