如何读取一个txt文件的内容?

wwyysha 2003-07-28 04:00:55
有一个txt文件,我想按行读取其中的内容,如何实现?请各位大虾帮忙!
...全文
80 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
flyfish365 2003-07-28
  • 打赏
  • 举报
回复
up
lemon_wei 2003-07-28
  • 打赏
  • 举报
回复
var

F: TextFile;
S: string;
begin
if OpenDialog1.Execute then { Display Open dialog box }
begin
AssignFile(F, OpenDialog1.FileName); { File selected in dialog }
Reset(F);
Readln(F, S); { Read first line of file }
Edit1.Text := S; { Put string in a TEdit control }
CloseFile(F);
end;
end;
jsandy 2003-07-28
  • 打赏
  • 举报
回复
procedure TForm1.Button1Click(Sender: TObject);
var
F1:TextFile;
Tmp_Str:String;
begin
AssignFile(F1,'c:\yourtxtfile.txt');
Reset(F1);
while not Eof(F1) do
begin
Readln(F1,Tmp_str);
ShowMessage(Tmp_Str);
end;
CloseFile(F1);
end;
tccb 2003-07-28
  • 打赏
  • 举报
回复
至少还有十种方法。。。
lvqiang 2003-07-28
  • 打赏
  • 举报
回复
同意 theone_jxm() ( ) 信誉:100
tongdings 2003-07-28
  • 打赏
  • 举报
回复
var
F: TextFile;
s: string;

begin
//test sFileName is exists?
if not FileExists(sFileName) then
Exit;

//Associate File
AssignFile(F,sFileName);

//Opens an existing file
Reset(F);

//First clear the memo
//Here Memo1 is Global var,and you can use Array
//or TMemoryStream var in stead of it.
if Memo1.lines.count > 0 then
Memo1.lines.clear;

//Read data per line
s := '';
while (not Eoln(F)) //File not end
begin
Readln(F,s);
Memo1.lines.Add(s);

s := '';
end; //end of [while (not Eoln(F))]

//Close File
CloseFile(F);
end;
  • 打赏
  • 举报
回复
var
a:Tstringlist;
begin
a:=Tstringlist.create;
a.loadfromfile('');
a.string[i]
a.free;
end;
fhuibo 2003-07-28
  • 打赏
  • 举报
回复
if not (FileExists(GetNameDir(Application.ExeName)+'111.txt')) then
begin
Application.MessageBox('xuanhao.txt文件不存在','提示',MB_OK+MB_ICONSTOP);
Exit;
end;
AssignFile(FileName,GetNameDir(Application.ExeName) + '111.txt');
Reset(FileName);
Readln(FileName,Str);
CloseFile(FileName);
web700 2003-07-28
  • 打赏
  • 举报
回复
解决了
努力
koma2003 2003-07-28
  • 打赏
  • 举报
回复
先声明一个变量MyTxt:Textfile;
再assignfile(Mytxt,'c:\1.txt');
然后Readln;
BeyondStudio 2003-07-28
  • 打赏
  • 举报
回复
procedure TForm1.Memo1Change(Sender: TObject);
begin
Memo1.Lines.LoadFromFile('C:\Autoexec.bat');
end;
wolf2005 2003-07-28
  • 打赏
  • 举报
回复
ReadLn

5,391

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧