var F: TextFile;flopen:boolean=false;procedure TForm1.Button1Click(Sender: TObject); begin if OpenDialog1.Execute then { Display Open dialog box } begin AssignFile(F, OpenDialog1.FileName); { File selected in dialog box } Reset(F);flopen:=true; end; end;procedure TForm1.Edit1Click(Sender: TObject); var s:string; begin if (flopen) and (not eof(f)) then begin readln(f,s); edit1.Text:=trim(s); end else messagedlg('文件未打开或者已经到达文件末尾',mtinformation,[mbok],0); end;
再给你抄一段你看看在Delphi6中,打开一个文件有多种方法,对于文本文件常用的方法是:先定义一个文本文件 变量,然后用AssignFile函数建立并联,以Reset函数打开文件并将指针放在文件头,最后用 Read或Readln函数逐行读出文件内容,并用EOF函数判断是否到达文件尾部。[/b] 例: procedure OpenFile var txtfile:TextFile; //定义文本文件变量 s:string; Begin AssignFile(txtfile,'Example.txt'); //建立关联 While Not Eof(txtFile) do begin readln(txtFile,s); memo1.lines.add(s); end; closeFile(txtFile); END;
var txtfile:TextFile; filename:TFilename; begin filename:=extractfilepath(application.exename)+'myfile.txt'; assign(txtfile,filename); if fileexists(filename) then append(txtfile) else rewrite(txtfile); writeln(txtfile,'lskfjwie=(("1251.3333.333"))'); closefile(txtfile);不过这种带表达式的最好用ini文件var ini:Tinifile; tempstr:string; begin //读取 ini:=Tinifile.create(extractfilepath(application.exename)+'myfile.ini'); try tempstr:=ini.readstring('小节名','变量名','默认值'); finally ini.free; end; //写 tempstr='值'; ini:=Tinifile.create(extractfilepath(application.exename)+'myfile.ini'); try ini.writestring('小节名','变量名',tempstr); finally ini.free; end;end;
也可以这样: procedure TForm1.FindClick(Sender: TObject); var Counter : Integer; i : Integer; s : String; List : TStringList; begin List := TStringList.Create; List.LoadFromFile( Path ); for Counter := 0 to List.Count - 1 do begin s := List[ Counter ]; i := pos( FindString, s ); if i <> 0 then Memo1.Lines.Add( Format( 'Find in Lines: %d, Columns: %d', [ Counter + 1 , i ] ) ); end; end;
F: TextFile;flopen:boolean=false;procedure TForm1.Button1Click(Sender: TObject);
begin
if OpenDialog1.Execute then { Display Open dialog box }
begin
AssignFile(F, OpenDialog1.FileName); { File selected in dialog box }
Reset(F);flopen:=true;
end;
end;procedure TForm1.Edit1Click(Sender: TObject);
var s:string;
begin
if (flopen) and (not eof(f)) then begin
readln(f,s);
edit1.Text:=trim(s);
end
else messagedlg('文件未打开或者已经到达文件末尾',mtinformation,[mbok],0);
end;
我用TstringList 特别方便TempStrLis:=TStringList.Create;
try
TempStrLis.LoadFromFile('.......telDetail.txt');
变量,然后用AssignFile函数建立并联,以Reset函数打开文件并将指针放在文件头,最后用
Read或Readln函数逐行读出文件内容,并用EOF函数判断是否到达文件尾部。[/b]
例:
procedure OpenFile
var
txtfile:TextFile; //定义文本文件变量
s:string;
Begin
AssignFile(txtfile,'Example.txt'); //建立关联
While Not Eof(txtFile) do
begin
readln(txtFile,s);
memo1.lines.add(s);
end;
closeFile(txtFile);
END;
filename:TFilename;
begin
filename:=extractfilepath(application.exename)+'myfile.txt';
assign(txtfile,filename);
if fileexists(filename) then
append(txtfile)
else
rewrite(txtfile);
writeln(txtfile,'lskfjwie=(("1251.3333.333"))');
closefile(txtfile);不过这种带表达式的最好用ini文件var ini:Tinifile;
tempstr:string;
begin
//读取
ini:=Tinifile.create(extractfilepath(application.exename)+'myfile.ini');
try
tempstr:=ini.readstring('小节名','变量名','默认值');
finally
ini.free;
end;
//写
tempstr='值';
ini:=Tinifile.create(extractfilepath(application.exename)+'myfile.ini');
try
ini.writestring('小节名','变量名',tempstr);
finally
ini.free;
end;end;
procedure TForm1.FindClick(Sender: TObject);
var
Counter : Integer;
i : Integer;
s : String;
List : TStringList;
begin
List := TStringList.Create;
List.LoadFromFile( Path );
for Counter := 0 to List.Count - 1 do
begin
s := List[ Counter ];
i := pos( FindString, s );
if i <> 0 then
Memo1.Lines.Add(
Format( 'Find in Lines: %d, Columns: %d',
[ Counter + 1 , i ] ) );
end;
end;
tstringlist 很方便,自己拆分的时候注意点。
搜索以前的帖子很多的