Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Read text file

Posted on 2006-03-28
3
Medium Priority
?
374 Views
Last Modified: 2010-04-05
I have a txt file look like this.

.jw1,jw1_file,"c:\windows\notepad.exe"
.lv2,jw1_file,"c:\windows\notepad.exe"
.jw2,jw2_file,"c:\windows\notepad.exe"
.lss,lss_file,"c:\app\ziper.exe"

I want to read line after line but capture position of “,”

First read set like this
Set1:=<.jw1>
Set2:=< jw1_file>
Set3:=<"c:\windows\notepad.exe">

Do something…..

Read next line and set next values from files.
0
Comment
Question by:Dark_King
3 Comments
 
LVL 14

Accepted Solution

by:
Pierre Cornelius earned 2000 total points
ID: 16311483
try this:

pas:
===
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, ComCtrls;

type
  TForm1 = class(TForm)
    ListView1: TListView;
    Panel1: TPanel;
    Label1: TLabel;
    inpFilename: TEdit;
    Button1: TButton;
    OpenDialog1: TOpenDialog;
    btnRead: TButton;
    procedure Button1Click(Sender: TObject);
    procedure btnReadClick(Sender: TObject);
  private
    procedure GetTextData(fname: String);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
  if OpenDialog1.Execute
    then inpFilename.Text:= OpenDialog1.FileName;
end;

procedure TForm1.GetTextData(fname: String);
var f: Textfile;
    s, set1, set2, set3: string;
    p: integer;
begin
  AssignFile(f, fname);
  reset(f);
  try
    while not eof(f) do
    begin
      readln(f, s);

      p:= pos(',', s);
      set1:= copy(s, 1, p-1);
      Delete(s, 1, p);

      p:= pos(',', s);
      set2:= copy(s, 1, p-1);
      Delete(s, 1, p);

      set3:= s;

      with ListView1.Items.Add do
      begin
        caption:= set1;
        SubItems.Add(set2);
        SubItems.Add(set3);
      end;
    end;
  finally
    closefile(f);
  end;
end;

procedure TForm1.btnReadClick(Sender: TObject);
begin
  if not FileExists(inpFilename.Text) then
    raise Exception.Create('Invlaid filename: '+inpFilename.Text);

  GetTextData(inpFilename.Text);
end;

end.


dfm:
=====
object Form1: TForm1
  Left = 192
  Top = 114
  Width = 696
  Height = 480
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object ListView1: TListView
    Left = 0
    Top = 41
    Width = 688
    Height = 405
    Align = alClient
    Columns = <
      item
        AutoSize = True
        Caption = 'Set1'
      end
      item
        AutoSize = True
        Caption = 'Set2'
      end
      item
        AutoSize = True
        Caption = 'Set3'
      end>
    TabOrder = 0
    ViewStyle = vsReport
  end
  object Panel1: TPanel
    Left = 0
    Top = 0
    Width = 688
    Height = 41
    Align = alTop
    TabOrder = 1
    DesignSize = (
      688
      41)
    object Label1: TLabel
      Left = 16
      Top = 8
      Width = 40
      Height = 13
      Caption = 'Input file'
    end
    object inpFilename: TEdit
      Left = 72
      Top = 8
      Width = 505
      Height = 21
      Anchors = [akLeft, akTop, akRight]
      TabOrder = 0
    end
    object Button1: TButton
      Left = 582
      Top = 7
      Width = 22
      Height = 22
      Anchors = [akTop, akRight]
      Caption = '…'
      Font.Charset = ANSI_CHARSET
      Font.Color = clWindowText
      Font.Height = -11
      Font.Name = 'Times New Roman'
      Font.Style = [fsBold]
      ParentFont = False
      TabOrder = 1
      OnClick = Button1Click
    end
    object btnRead: TButton
      Left = 616
      Top = 8
      Width = 68
      Height = 25
      Caption = 'Read'
      TabOrder = 2
      OnClick = btnReadClick
    end
  end
  object OpenDialog1: TOpenDialog
    Left = 304
  end
end


Regards
Pierre
0
 
LVL 3

Expert Comment

by:dygj
ID: 16311530
try using tstringlist and delimitedtext.

small ex here. You need to write code to read the file and loop trough all lines.
but this should give you a start.

var
b:tstringlist;
c,d,e:string;
begin
try
b:=tstringlist.create;
b:delimiter:=',';
b.delimitedtext:='add your line here, and create a loop handling all your lines';
// show each item
c:=b[0];
d:=b[1];
e:=b[2];
showmessage('Extention: '+c+' Type: '+d+'  File name:'+e)
finally
freeandnil(b);
end;
end;

Reply if you need more info about reading the file itselfs.
0
 
LVL 12

Expert Comment

by:esoftbg
ID: 16314856
//................................ Unit ................................

unit Unit1_Q_21791945;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics,
  Controls, Forms, Dialogs, Grids, StdCtrls, Buttons;

type
  TForm1 = class(TForm)
      MemoTxt: TMemo;
      Memo1: TMemo;
      Memo2: TMemo;
      Memo3: TMemo;
      BitBtn1: TBitBtn;
      procedure FormCreate(Sender: TObject);
      procedure BitBtn1Click(Sender: TObject);
    private
      FNT:    string;
    { Private declarations }
    public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  FNT := ExtractFilePath(Application.ExeName) + 'Text.txt';
  MemoTxt.Lines.LoadFromFile(FNT);
end;

procedure TForm1.BitBtn1Click(Sender: TObject);
var
  I:      Integer;
  J:      Integer;
  K:      Integer;
  L:      Integer;
  LS:     Integer;
  ST:     string;
  SETS:   string;
  procedure Add_Line_into_Memo(K: Integer);
  begin
    SetLength(SETS, LS);
    case K of
      1: Memo1.Lines.Add(SETS);
      2: Memo2.Lines.Add(SETS);
      3: Memo3.Lines.Add(SETS);
    end;
  end;
begin
  for I := 0 to MemoTxt.Lines.Count-1 do
  begin
    K := 1;
    LS := 0;
    ST := MemoTxt.Lines[I];
    L := Length(ST);
    SetLength(SETS, L);
    for J := 1 to L do
      SETS[J] := #32;
    for J := 1 to L do
    begin
      if (ST[J]=',') then
      begin
        Add_Line_into_Memo(K);
        SetLength(SETS, L);
        LS := 0;
        Inc(K);
      end
      else
      begin
        Inc(LS);
        SETS[LS] := ST[J];
      end;
      if (J=L) then
        Add_Line_into_Memo(K);
    end;
  end;
  Memo1.Lines.SaveToFile('Set1.txt');
  Memo2.Lines.SaveToFile('Set2.txt');
  Memo3.Lines.SaveToFile('Set3.txt');
end;

end.

//................................ Form ................................

object Form1: TForm1
  Left = 216
  Top = 112
  BorderIcons = [biSystemMenu, biMinimize]
  BorderStyle = bsSingle
  Caption = 'Form1'
  ClientHeight = 542
  ClientWidth = 760
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  OnCreate = FormCreate
  PixelsPerInch = 96
  TextHeight = 13
  object MemoTxt: TMemo
    Left = 4
    Top = 48
    Width = 256
    Height = 480
    TabOrder = 0
  end
  object Memo1: TMemo
    Left = 264
    Top = 48
    Width = 104
    Height = 480
    ReadOnly = True
    TabOrder = 1
  end
  object Memo2: TMemo
    Left = 372
    Top = 48
    Width = 136
    Height = 480
    ReadOnly = True
    TabOrder = 2
  end
  object Memo3: TMemo
    Left = 512
    Top = 48
    Width = 232
    Height = 480
    ReadOnly = True
    TabOrder = 3
  end
  object BitBtn1: TBitBtn
    Left = 176
    Top = 16
    Width = 80
    Height = 25
    Caption = 'Make Sets =>'
    TabOrder = 4
    OnClick = BitBtn1Click
  end
end
0

Featured Post

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.

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…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses

810 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