#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Posts
    47
    Rep Power
    11

    Working with txt files


    Im creating a Delphi 5 App with a Mysql DB backend, that deals with Contacts Details.

    The user would like to import contacts from a .txt file into the DB using delphi App.

    Im ok with connecting to the DB and running insert queries

    is it be possible to import the txt file data into an array or string list which can then be looped through to run the insert query

    The txt file would be in the following format:

    "John","Smith","01272333222"
    "Matt","Jones","01221122353"
    "Chris","James","01382733212"
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,648
    Rep Power
    4248
    Maybe you can use a TStringList?
    Code:
    var
       stl : TStringList;
       i : integer;
       sLine : string;
    begin
       stl := TStringList.Create;
       stl.LoadFromFile('C:\path\to\file.txt');
       for i := 0 to stl.Count - 1 do
       begin
          sLine := stl[i];
          // Do something with sLine here.
       end;
       stl.Free;
    end;
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2004
    Posts
    1
    Rep Power
    0

    Wink Better way of control any incomming text


    The txt file would be in the following format:

    "John","Smith","01272333222"
    "Matt","Jones","01221122353"
    "Chris","James","01382733212"[/QUOTE]

    My code is more complex but you gain complete control over the textfile to do what ever you like.

    Use text incomming in fixed possition.

    Record structure
    1 - 10 on line is field 1
    11 - 20 is field 2
    21 - 30 is field 3

    and so on.

    Place the file at your harddrive c:\Example.txt
    Here is my test in the file:
    Text1 Text2 Text3

    Start delphi and have an emty project with one emty panel
    Place a Button and one memo filed on it.

    Add my code where you get when you double click on the button.

    My code under procedure.
    var
    F: TextFile;
    S: string;
    XYField1, XYField2, XYField3 : String;
    LineStep, LineLenght, X : Integer;
    Letter : STRING[1];
    begin
    StatusBar1.SimpleText := 'Working';
    AssignFile(F, 'C:\Example.txt'); { Collect file 'ANY FILE'}
    Reset(F); { Place read pointer to beginning of file }
    While not EOF(F) Do
    Begin
    Readln(F, S); { Read the first line out of the file }
    {Check the record lengt}
    LineLenght := Length(S);
    LineStep := LineStep + 1;
    {Collect Field one in the fixed position
    I know it is a pascal command for this but cant remeber, so i use the FOR loop instead}
    FOR X := 1 to 10 Do
    XYField1 := XYField1 + S[X];
    {Collect field two}
    FOR X := 11 to 20 Do
    XYField2 := XYField2 + S[X];
    {Collect field tre}
    FOR X := 21 to 30 Do
    XYField2 := XYField2 + S[X];
    { Move the fields to the report query you are building up}
    { i just move it to a Memo field }
    Memo1.Lines.add('Field1:' + XYField1 + ' Field2:' + XYField2 + ' Field3:' + XYField3);

    End;
    CloseFile(F);
    end;
    END.

IMN logo majestic logo threadwatch logo seochat tools logo