Importing data from a TXT to a StringGrid

3

I have a stringgrid , and I need to import a txt file into it, however I need to grab all the lines according to the positions, and I have no idea how to do it, I did research, but without success.

I need this kind of code to tell me how to compare two stringgrids with a DLL from a fiscal printer, and I have to copy those columns respectively according to their position.

    
asked by anonymous 06.05.2014 / 16:36

1 answer

4

I'm not in favor of giving the fish to the crowd, so I'll try to teach you how to fish:

We have the following text file, called test.txt ( which should be in the same folder as the executable, to facilitate our test ):

123456789012345678901234567890
COL1      COL2      COL3      
DADO1     DADO2     DADO3     
TESTE1    TESTE2    TESTE3    

Note that the 123456789012345678901234567890 line should not go to the text file, consider it a ruler, assuming that the TXT you are working on is typed.

Considering that your txt is small, we will use a StringList to capture the data of it as follows:

procedure TForm1.Button1Click(Sender: TObject);
var 
  arquivo : TStringList;
  i : integer;
begin
  arquivo := TStringList.Create;
  arquivo.LoadFromFile('teste.txt');
end;

Now your file is all in StringList File and is ready to be compared with any value as follows:

  for i := 0 to arquivo.Count - 1 do
  begin
    Showmessage('Primeiro campo: ' + copy(arquivo[i],1,10));   //Primeiro Campo
    Showmessage('Segundo campo: ' + copy(arquivo[i],11,10));  //Segundo Campo
    Showmessage('Terceiro campo: ' + copy(arquivo[i],21,10));  //Terceiro Campo
  end;

From here on you get the insertion in StringGrid , the validation and use of the DLL of Bematech, Daruma or whatever the manufacturer. The created loop will go through all StringList . Remember that it starts at 0 and goes to arquivo.count - 1 by the index of TStringList if it starts at 0 and not at 1.

    
06.05.2014 / 17:19