?
Solved

A better solution / algoritm

Posted on 2003-02-25
14
Medium Priority
?
133 Views
Last Modified: 2010-04-04
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
Comment
Question by:tscooter82
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 3
  • +3
14 Comments
 
LVL 2

Expert Comment

by:j42
ID: 8014982
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
 
LVL 3

Expert Comment

by:ILE
ID: 8014985
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
 
LVL 2

Expert Comment

by:j42
ID: 8015011
Darn, no Umlaute: Tschüß -> Tschuess
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 21

Expert Comment

by:ziolko
ID: 8015018
why use TMemo?? TStringList will be enough.
ziolko.
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 8015019
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
 
LVL 2

Expert Comment

by:j42
ID: 8015036
well, ILE did the coding so points belong to him ;-)
Hey, what about releasing caps lock!

Cheers
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 8015104
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
 
LVL 9

Accepted Solution

by:
mocarts earned 400 total points
ID: 8015915
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
 
LVL 9

Expert Comment

by:mocarts
ID: 8015920
in line where reading goes on must be:
fs.Read(fr, sizeOf(fr));

mo.
0
 

Author Comment

by:tscooter82
ID: 8016655
Comment from mocarts!!

Why dosnt it read in scandinavian chars? ÅÄÖ
0
 
LVL 9

Expert Comment

by:mocarts
ID: 8016812
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
 

Author Comment

by:tscooter82
ID: 8018862
I use paradox
0
 
LVL 9

Expert Comment

by:mocarts
ID: 8027606
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
 

Author Comment

by:tscooter82
ID: 8028122
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses
Course of the Month13 days, 17 hours left to enroll

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question