Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 134
  • Last Modified:

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

0
tscooter82
Asked:
tscooter82
  • 4
  • 3
  • 3
  • +3
1 Solution
 
j42Commented:
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
0
 
ILECommented:
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}


0
 
j42Commented:
Darn, no Umlaute: Tschüß -> Tschuess
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
ziolkoCommented:
why use TMemo?? TStringList will be enough.
ziolko.
0
 
kretzschmarCommented:
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 ;-)
0
 
j42Commented:
well, ILE did the coding so points belong to him ;-)
Hey, what about releasing caps lock!

Cheers
0
 
kretzschmarCommented:
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 ;-)
0
 
mocartsCommented:
implementation

type
  TProdRec = record
    res1: array [0..1] of char; // unused
    pno: array [0..6] of char; // product number
    res2: array [0..13] of char; // unused
    pna: array [0..34] of char; // product name
    ptl: array [0..34] of char;  // product something :)
    res3: array [0..140] of char; // including CRLF
  end;

procedure TForm1.Button1Click(Sender: TObject);
var
  fr: TProdRec;
  fs: TFileStream;
begin
  if OpenDialog1.Execute then begin
    fs := TFileStream.Create(openDialog1.FileName, fmOpenRead);
    try
      fs.Seek(50, soFromBeginning); // skipping header;
      while fs.Position < fs.Size do begin
        fs.Read(fr, sizeOf(tfr));
        tableProdukter.Append;
        tableProdukterProduktnummer.Text := fr.pno;
        tableProdukterProduktnamn.Text :=
          StringReplace(fr.pna, ' ', Chr(160), [rfReplaceAll]);
        tableProdukterTillgsuppgiter.Text :=
          StringReplace(fr.ptl, ' ', Chr(160), [rfReplaceAll]);
        tableProdukter.Post;
      end;
    finally
      fs.free;
    end;
  end;
end;

wbr, mo.
0
 
mocartsCommented:
in line where reading goes on must be:
fs.Read(fr, sizeOf(fr));

mo.
0
 
tscooter82Author Commented:
Comment from mocarts!!

Why dosnt it read in scandinavian chars? ÅÄÖ
0
 
mocartsCommented:
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.
0
 
tscooter82Author Commented:
I use paradox
0
 
mocartsCommented:
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.
0
 
tscooter82Author Commented:
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!!!
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 3
  • 3
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now