16,748
社区成员
发帖
与我相关
我的任务
分享
SH 张三 23124
SH 李四 13000
SQ 王五 12312
SZ 小四 34256
HK 小五 12545
HK 七七 8954
SH 阿飞 15542
procedure TForm1.FormCreate(Sender: TObject);
var
TextFileVar:TextFile;
office,name,salary,temp:String;
begin
AssignFile(TextFileVar,'test.txt'); //关联文件
Reset(TextFileVar); //打开文件
Edit1.Text:='';
while not Eof(TextFileVar) do begin
Read(TextFileVar,office,name,salary); //读取一行数据
if((office='SZ')or( office='SH')or( office='HK')) then
begin
Edit1.Text:=Edit1.Text+(office+name+salary);
break;
end;
end;
CloseFile(TextFileVar);
Edit1.Text:=Edit1.Text+(temp);
end;
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Grids, DBGrids, DB, ADODB, DBClient, StdCtrls;
type
TForm1 = class(TForm)
DBG_1: TDBGrid;
DS_1: TDataSource;
ds1: TClientDataSet;
btn1: TButton;
mmo1: TMemo;
procedure btn1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.btn1Click(Sender: TObject);
var
Str: String;
f: TextFile;
sL: TStringList;
CDS: TClientDataSet;
begin
AssignFile( f, 'TEST.txt' );
Reset( f );
sL:= TStringList.Create;
sL.Delimiter:= ' ';
CDS:= TClientDataSet.Create(Self);
CDS.FieldDefs.Add('Addr', ftString, 8, True);
CDS.FieldDefs.Add('Name', ftString, 8, True);
CDS.FieldDefs.Add('Salary', ftString, 8, True);
CDS.CreateDataSet;
CDS.Open;
while not EOF( f ) do
begin
Readln( f, Str );
sL.DelimitedText:= Str;
if( ( sL[0]='SZ' )or( sL[0]='SH' )or( sL[0]='HK' ) ) then
begin
mmo1.Lines.Add( sL[0]+'+'+sL[1]+'+'+sL[2] );
end;
CDS.Append;
CDS.FieldByName('Addr').AsString := sL[0];
CDS.FieldByName('Name').AsString := sL[1];
CDS.FieldByName('Salary').AsString := sL[2];
CDS.Post;
end;
DS_1.DataSet:= CDS;
DBG_1.DataSource:= DS_1;
sL.Free;
CloseFile( f );
end;
end.