procedure TForm1.BitBtn1Click(Sender: TObject); var F:textFile; I:integer; S:string; begin AssignFile(F,'C:\dd.txt'); Reset(F); I:=1; while not eof(F) do begin readln(F,s); self.StringGrid1.Rows[I].Delimiter:=chr(9); //chr(9)是个TAB键值 self.StringGrid1.Rows[I].DelimitedText:=s; inc(I); end; closeFile(F); end;
Sample Name:D-8342 Data1 C A 1 0.342 234 2 0.322 423 \\说一下具体的规则吧,如果只读D-8342和0.322那很简单, 就这个文件论这个文件, 把第一行读入一个String类型的变量line1中,然后line1:=copy(line1,pos('D',line1'),length(line1)-1)就能得到D-8342;下面要得到0.322不用我多说了吧?
unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, StrUtils;type TForm1 = class(TForm) Button1: TButton; Edit1: TEdit; Edit2: TEdit; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end;var Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject); var i:integer; s:string; s1:TStringList; begin s1:=TStringList.Create; s1.LoadFromFile('C:\1.txt'); for i:=0 to s1.Count-1 do begin if pos('Sample Name:',s1[i])>0 then begin s:=trim(copy(s1[i],pos('Sample Name:',s1[i])+12,length(s1[i]))); if pos(' ',s)>0 then s:=LeftStr(s,pos(' ',s)-1); Edit1.Text:=s; end; if AnsiStartsStr('2 ',s1[i]) then begin s:=trim(copy(s1[i],3,length(s1[i]))); s:=LeftStr(s,pos(' ',s)-1); Edit2.Text:=s; break; end; end; s1.Free; end;end.
var
F:textFile;
I:integer;
S:string;
begin AssignFile(F,'C:\dd.txt');
Reset(F);
I:=1;
while not eof(F) do
begin
readln(F,s);
self.StringGrid1.Rows[I].Delimiter:=chr(9); //chr(9)是个TAB键值
self.StringGrid1.Rows[I].DelimitedText:=s;
inc(I);
end;
closeFile(F);
end;
Data1 C A
1 0.342 234
2 0.322 423
\\说一下具体的规则吧,如果只读D-8342和0.322那很简单,
就这个文件论这个文件,
把第一行读入一个String类型的变量line1中,然后line1:=copy(line1,pos('D',line1'),length(line1)-1)就能得到D-8342;下面要得到0.322不用我多说了吧?
S/N:23424232234
Sample Name:D-8342 InjectDate:2005-08-14
something error!
Data1 C A
1 0.342 234
2 0.322 423
然后我还想读原来的值呢,
或者说我想读的值是
1."Sample Name:"后的字符串"D-8342",不管它在哪一行;
2.读出"Data1"后第2行第2列的数"0.322",不管"Data1"在哪里?
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, StrUtils;type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
Edit2: TEdit;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
var i:integer;
s:string;
s1:TStringList;
begin
s1:=TStringList.Create;
s1.LoadFromFile('C:\1.txt');
for i:=0 to s1.Count-1 do
begin
if pos('Sample Name:',s1[i])>0 then
begin
s:=trim(copy(s1[i],pos('Sample Name:',s1[i])+12,length(s1[i])));
if pos(' ',s)>0 then s:=LeftStr(s,pos(' ',s)-1);
Edit1.Text:=s;
end;
if AnsiStartsStr('2 ',s1[i]) then
begin
s:=trim(copy(s1[i],3,length(s1[i])));
s:=LeftStr(s,pos(' ',s)-1);
Edit2.Text:=s;
break;
end;
end;
s1.Free;
end;end.
文本文件一定要有固定格式,不然就要用全文查找了。
一是自己定义格式,二是用文本数据库格式(《d5开发指南》这本书里有)