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);
tableProdukterProduktnumme r.Text:=ta bleProdukt erProduktn ummer.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:=tabl eProdukter Produktnam n.Text+Chr (160)
else
tableProdukterProduktnamn. Text:=tabl eProdukter Produktnam n.Text+ch;
end;
a:=0;
while a<35 do
begin
a:=a+1;
Read(F1, Ch);
if Ch=' ' then tableProdukterTillgsuppgit er.Text:=t ableProduk terTillgsu ppgiter.Te xt+Chr(160 )
else
tableProdukterTillgsuppgit er.Text:=t ableProduk terTillgsu ppgiter.Te xt+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 00150000006400000000006000 0006000000 0000000000 0000000000 000000000 00V M 000010000
RL0101105 2SWE20020521SG-TYTON TRYCKRÖR DN 90 SG-TYTON AA002 10105 M 00150000006400000000006000 0006000000 0000000000 0000000000 000000000 00V M 000010000
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);
tableProdukterProduktnumme
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.
else
tableProdukterProduktnamn.
end;
a:=0;
while a<35 do
begin
a:=a+1;
Read(F1, Ch);
if Ch=' ' then tableProdukterTillgsuppgit
else
tableProdukterTillgsuppgit
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 00150000006400000000006000
RL0101105 2SWE20020521SG-TYTON TRYCKRÖR DN 90 SG-TYTON AA002 10105 M 00150000006400000000006000
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(OPENDIA LOG.FILANA ME);
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}
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(OPENDIA
FOR I:=0 TO MEMO1.LINES.COUNT-1 DO
BEGIN
TABLE1.APPEND;
S:=MEMO1.LINES[I];
TABLE1.......TEXT:=COPY(S,
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
why use TMemo?? TStringList will be enough.
ziolko.
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 ;-)
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
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 ;-)
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
in line where reading goes on must be:
fs.Read(fr, sizeOf(fr));
mo.
fs.Read(fr, sizeOf(fr));
mo.
ASKER
Comment from mocarts!!
Why dosnt it read in scandinavian chars? ÅÄÖ
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.
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.
ASKER
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.
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.
ASKER
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!!!
I also checked the charset and it was set to Default_Charset.
oooh noo stupid me, the fault was in the text file!!!
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