Link to home
Start Free TrialLog in
Avatar of tscooter82
tscooter82

asked on

A better solution / algoritm

Does anyone hav a better solution for this code! It's reading data from a text file and inserting it to a table.

b:=0;
if OpenDialog1.Execute then
begin
    AssignFile(F1, OpenDialog1.Filename);
    Reset(F1);
while not eof(f1) do
begin

a:=0;
if b=0 then
begin
while a<50 do
begin
a:=a+1;
read(F1, Ch);
end;
b:=1;
end;

tableProdukter.Edit;
tableProdukter.Insert;
a:=0;
      while a<2 do
      begin
        a:=a+1;
        Read(F1, Ch);
      end;
a:=0;
      while a<7 do
      begin
        a:=a+1;
        Read(F1, Ch);
        tableProdukterProduktnummer.Text:=tableProdukterProduktnummer.Text+ch;
      end;
a:=0;
      while a<14 do
      begin
        a:=a+1;
        Read(F1, Ch);
      end;
a:=0;
      while a<35 do
      begin
        a:=a+1;
        Read(F1, Ch);
        if Ch=' ' then tableProdukterProduktnamn.Text:=tableProdukterProduktnamn.Text+Chr(160)
        else
        tableProdukterProduktnamn.Text:=tableProdukterProduktnamn.Text+ch;
      end;
a:=0;
      while a<35 do
      begin
        a:=a+1;
        Read(F1, Ch);
        if Ch=' ' then tableProdukterTillgsuppgiter.Text:=tableProdukterTillgsuppgiter.Text+Chr(160)
        else
        tableProdukterTillgsuppgiter.Text:=tableProdukterTillgsuppgiter.Text+ch;
      end;
a:=0;
      while a<141 do
      begin
        a:=a+1;
        Read(F1, Ch);
      end;
end;
CloseFile(F1);
end;

EXAMPLE OF THE TEXTFILE

OBYXXXXXXXXXXX      L01
OSE003710712079     L01
RL0101104  2SWE20020521SG-TYTON TRYCKRÖR                  DN  80                             SG-TYTON            AA002  10105 M  00150000006400000000006000000600000000000000000000000000000000000   00V                    M  000010000
RL0101105  2SWE20020521SG-TYTON TRYCKRÖR                  DN  90                             SG-TYTON            AA002  10105 M  00150000006400000000006000000600000000000000000000000000000000000   00V                    M  000010000

Avatar of j42
j42

Hi,

just some thoughts:
You can use a TMemo (maybe invisible) to read the file (LoadFromFile method). Once the file is loaded you can use TMemo.Lines.Count (= number of lines) for you loop. Access lines in Memo via Memo1.Lines[index]. To extract the desired substrings use the Copy() function (-> Delphi help).

Regards und Tschüß
J
WERY EASY



FIRST  PUT ONE MEMO COMPONENT PIT BOUTH SCROLLBARS TO TRUE

ON THE FORM AND DO THIS (MEMO COMPONENT MEY BE  INVISIBLE)


VAR S:STRING;
    I:INTEGER;


IF OPENDIALOG.EXECUTE THEN
BEGIN
MEMO1.LOADFROMFILE(OPENDIALOG.FILANAME);
FOR I:=0 TO MEMO1.LINES.COUNT-1 DO
BEGIN
 TABLE1.APPEND;
 S:=MEMO1.LINES[I];
 TABLE1.......TEXT:=COPY(S,X,Y)
END;

  ...... IS THE NAME OF FIELD
 {WHERE X IS A NUMER OF START CARACTER AND Y IS A NUMBER OF CARACTER TO LOAD}


Darn, no Umlaute: Tschüß -> Tschuess
Avatar of Lukasz Zielinski
why use TMemo?? TStringList will be enough.
ziolko.
if you use the bde,
then you can use tbatchmove

->just create a schema file, where the fields are defined

see bde-online-help
section
Binding external schema to text tables

meikl ;-)
well, ILE did the coding so points belong to him ;-)
Hey, what about releasing caps lock!

Cheers
hmm, i see,
you have different records in one file,
then tbatchmove cannot work with

but if you have control of the textfile-output,
then place a record-identifier in front of each
line

meikl ;-)
ASKER CERTIFIED SOLUTION
Avatar of mocarts
mocarts

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
in line where reading goes on must be:
fs.Read(fr, sizeOf(fr));

mo.
Avatar of tscooter82

ASKER

Comment from mocarts!!

Why dosnt it read in scandinavian chars? ÅÄÖ
code do not read or data not displayed correctly?

what type of database you use? (paradox, oracle etc?)
is your BDE alias configured for correct Language?
if you use paradox you must ensure that your tables have correct code page.
mo.
I use paradox
can you tell me in what exactly place you don't see correct characters?
in what code page is that data file?

you can additionaly check in BDE Administrator under Configuration page:
  Configuration -> Drivers -> Native -> Paradox option Language Driver
for Windows scandinavian character sets there is "Pdox ANSI nordan40" (Norvegian/Danish) and "Pdox ANSI SwedFin" (Swedish/Finnish)
for DOS code pages - Paradox 'nordan40' and Paradox 'swedfin'

also check font charset you use in grid (if any) when display data.

wbr, mo.
I checked the BDE admin and it was set to SwedFin.
I also checked the charset and it was set to Default_Charset.

oooh noo stupid me, the fault was in the text file!!!