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

    Join Date
    Nov 2004
    Location
    Australia, Sydney
    Posts
    12
    Rep Power
    0

    Talking DELPHI - Extracting Data (From Text Files)


    Hi Delphi Guru's,

    I am trying to throw together a Delphi application where i can boot up this application and load in a .txt file which contains a few thousand rows of crap.

    What i want to do is extract data from the 1st to 10th line of the file.

    Say on line 2, it has a phone number, so from postion 1 to 14 i want to throw all that data into a variable for further processing. Then from position 15-50 it has the persons full name, and i also want to get this data, etc etc etc.....

    My main concern is in delphi, how can i grab data from a certain line and certain location of a text file, so that i can use it for further processing later. And then inserting the data into a database....

    If im unclear just let me know and i'll try and explain better

    Thanks
    Cheeky Chino
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,607
    Rep Power
    4247
    1. Load the data into a StringList object using the LoadFromFile method.
    2. Iterate through the string list.
    3. For each line, use the Copy() function to copy characters from a specific position.
    4. Repeat step 2 until you have enough lines.
    5. Free the stringlist object.

    Code:
    var
        stl : TStringList;
        i : integer;
        sLine, sPhone, sFullName : string;
    begin
        stl := TStringList.Create;
        stl.LoadFromFile('C:\path\to\file.txt');
        // Do something with first 10 lines. If you want all the lines
        // processed, use stl.Count instead of 9 below.
        for i := 0 to 9 do
        begin
            sLine := stl.Strings[i];
            sPhone := Copy(sLine, 1, 10); // Copy 10 chars 
            sFullName := Copy(sLine, 11, 40); // Copy 40 chars 
            // Do something here with sPhone and sFullName
        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
    Nov 2004
    Location
    Australia, Sydney
    Posts
    12
    Rep Power
    0

    Talking


    Ok i've had a look at your code and modifed it a little and made myself understand whats happening

    Code:
      if OpenDialog1.Execute then
      begin
        stl := TStringList.Create;
        stl.LoadFromFile(OpenDialog1.FileName);
        for i := 0 to 3 do
        begin
            sLine := stl.Strings[i];
            sName := Copy(sLine, 1, 22);
            sPhone := Copy(sLine, 23, 14);
            sGender := Copy(sLine, 37, 7);
    
            ShowMessage('|| '+ sName +' || '+sPhone +' || '+ sGender +' ||');
        end;
        FreeAndNil(stl);
        //stl.Free;
      end;

    Although i see this way is loading the whole file into memory, but i may be loading 10mb files, and wont that make the application pretty resource hungry?

    Can i do it so that instead of loading the whole file into memory i can read it line by line?

    Or in your oppinion which way is better?
  6. #4
  7. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,607
    Rep Power
    4247
    If you want to read line by line, you can use AssignFile and ReadLn to do this. The online help documentation should show you what to do here.
    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

IMN logo majestic logo threadwatch logo seochat tools logo