一行一行的读取文本文件到edit1中(200分)

  • 主题发起人 主题发起人 ljbXS
  • 开始时间 开始时间
L

ljbXS

Unregistered / Unconfirmed
GUEST, unregistred user!
一行一行的读取文本文件到edit1中<br>文本文件格式,内容如下:<br>asads<br>dfd<br>fghfg<br>yjh<br>tryt<br>jjhh<br>......<br>我想每次从文件中读取一行,也就是一个字符串到edit1中,
 
先把文件Load进Memo中,然后再一行一行地处理。
 
将文本存和在tstrings中,进行读取.<br><br>edit1.text:=strings
 
用text文件类型就行了。
 
var<br>&nbsp; F: TextFile;flopen:boolean=false;<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>begin<br>&nbsp; if OpenDialog1.Execute then &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{ Display Open dialog box }<br>&nbsp; begin<br>&nbsp; &nbsp; AssignFile(F, OpenDialog1.FileName); &nbsp; { File selected in dialog box }<br>&nbsp; &nbsp; Reset(F);flopen:=true;<br>&nbsp; end;<br>end;<br><br>procedure TForm1.Edit1Click(Sender: TObject);<br>var s:string;<br>begin<br>&nbsp; if (flopen) and (not eof(f)) then begin<br>&nbsp; &nbsp; readln(f,s);<br>&nbsp; &nbsp; edit1.Text:=trim(s);<br>&nbsp; end<br>&nbsp; else messagedlg('文件未打开或者已经到达文件末尾',mtinformation,[mbok],0);<br>end;
 
如果中途要终止,也就是要到文件底,应该怎么写?
 
如果中途要终止,也就是要到文件底,应该怎么写?
 
处理这种文本文件, 我觉得最好的方法就是用TStringList,用LoadFromFile读到内存<br>中来,要怎么处理就怎么处理,多爽。<br>sl:=TStringList.Create;<br>try<br>&nbsp; sl.LoadFromFile('aaa.txt');<br>&nbsp; for i:=0 to sl.Count-1 do<br>&nbsp; &nbsp; Edit1.Text:=sl.strings;<br>finally<br>&nbsp; sl.Free;<br>end;
 
同意教父,用TstringList最方便
 
听说tstringslist特占资源
 
用TstringList应该是较好的办法
 
为什么TStringList特占资源?我好象不觉得。
 
如果中途不想读了,想终止就直接 CloseFile 好了,想跳到结尾就用 Readln 配合 Eof 好了。
 
to教父<br>如果数据比较大的话就比较明显,一般情况下我也感觉不到。
 
to iseek:那是,因为TStringList是把所有的内容都读到内存嘛 :)
 
//1. 在Form上再放一个Edit2,等待录入行数目。<br><br>//2. 建立装入过程:<br>// 申明全局变量:<br>&nbsp;Var<br>&nbsp; &nbsp; &nbsp;TxtLineS &nbsp;: Word; // 放文本总行数目<br>&nbsp; &nbsp; &nbsp;sl:TStringList;<br>Procedure LoadTxtFile;<br>begin<br>&nbsp; &nbsp; &nbsp; &nbsp;sl:=TStringList.Create;<br>&nbsp; &nbsp; &nbsp;try<br>&nbsp; &nbsp; &nbsp; &nbsp;sl.LoadFromFile('c:/aaa.txt');<br>&nbsp; &nbsp; &nbsp; &nbsp;TxtLineS:=sl.Count;<br>&nbsp; &nbsp; &nbsp;except<br>&nbsp; &nbsp; &nbsp; &nbsp;showmessage('不能打开文件 !');<br>&nbsp; &nbsp; &nbsp;end;<br>end;<br><br>//3. 在Edit2中输入需要到的行后&lt;Enter&gt;:<br>procedure TForm1.Edit2KeyPress(Sender: TObject; var Key: Char);<br>&nbsp;var<br>&nbsp; &nbsp;_LNo: Integer;<br>&nbsp;Begin<br>&nbsp; &nbsp; &nbsp; if key&lt;&gt;#13 then exit; // 不是Enter键!<br>&nbsp; &nbsp; &nbsp; _LNo:=-1;<br>&nbsp; &nbsp; try<br>&nbsp; &nbsp; &nbsp; _LNo:=StrToInt(Trim(Edit2.Text));<br>&nbsp; &nbsp; except<br>&nbsp; &nbsp; &nbsp; showmessage('无效的行数值 !');<br>&nbsp; &nbsp; end;<br><br>&nbsp; &nbsp; if (_LNo&lt;=0) then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; showmessage('无效的行数值 !');<br>&nbsp; &nbsp; &nbsp; exit;<br>&nbsp; &nbsp; end;<br>&nbsp; &nbsp; if (_LNo&gt;TxtLineS) then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; showmessage('已经超过文本的行数目,最大行为:'+Trim(IntToStr(TxtLineS))+' !');<br>&nbsp; &nbsp; &nbsp; exit;<br>&nbsp; &nbsp; end;<br>&nbsp; &nbsp; edit1.text:=sl.Strings[_LNo-1];<br>end;<br><br>把比的分给我吧,谢谢!!!<br>
 
ljbXS:<br>&nbsp; &nbsp; 你好!<br>&nbsp; &nbsp; 我猜想你要实现的是不能用一个FOR循环来实现的,那样一闪而过,根本就没有意义!<br>你可能是想点击一下鼠标读入一行,再点一次又读入另一行吧(如第一次点击按钮读文<br>本文件的第一行,第二次点击按钮读第二行),如果是这样,请看我写的程序代码:<br>{首先在C盘根目录下建立一文本文件,名为kkk.txt,内容如下:<br>1111111111111<br>2222222222222<br>3333333333333<br>4444444444444<br>5555555555555<br>.............<br>}<br><br>unit Unit1;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,<br>&nbsp; StdCtrls;<br><br>type<br>&nbsp; TForm1 = class(TForm)<br>&nbsp; &nbsp; Button1: TButton;<br>&nbsp; &nbsp; Edit1: TEdit;<br>&nbsp; &nbsp; procedure FormCreate(Sender: TObject);<br>&nbsp; &nbsp; procedure FormDestroy(Sender: TObject);<br>&nbsp; &nbsp; procedure Button1Click(Sender: TObject);<br>&nbsp; private<br>&nbsp; &nbsp; { Private declarations }<br>&nbsp; public<br>&nbsp; &nbsp; { Public declarations }<br>&nbsp; end;<br><br>var<br>&nbsp; Form1: TForm1;<br>&nbsp; i : integer; &nbsp;//定义循环控制变量<br>&nbsp; sl :TStringlist; &nbsp;//定义字符串列表变量<br><br>implementation<br><br>{$R *.DFM}<br><br>procedure TForm1.FormCreate(Sender: TObject);<br>begin<br>&nbsp; sl :=TStringList.Create ;<br>end;<br><br>procedure TForm1.FormDestroy(Sender: TObject);<br>begin<br>&nbsp; sl.Free ;<br>end;<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>const<br>&nbsp; txtFile='c:/kkk.txt';<br>begin<br>&nbsp; if sl.Count =0 then<br>&nbsp; &nbsp; if fileExists(txtFile) then<br>&nbsp; &nbsp; &nbsp; sl.LoadFromFile(txtFile);<br>&nbsp; if i=sl.Count then<br>&nbsp; begin<br>&nbsp; &nbsp; i :=0;<br>&nbsp; &nbsp; //读到文本文件的最后,又从开头读入第一行<br>&nbsp; &nbsp; Button1.click;<br>&nbsp; end<br>&nbsp; else<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; edit1.text :=sl.Strings;<br>&nbsp; &nbsp; &nbsp; i :=i+1;<br>&nbsp; &nbsp; end;<br>end;<br>end.<br>
 
var<br>f:system.textfile;<br>s:string;<br>wj:string;<br>maxwidth,i:integer;<br>begin<br>opendialog1.filter:='文本文件(*.txt;*.*)|*.txt;*.*';<br>try<br>if opendialog1.execute then &nbsp;//用打开对话框打开文本!<br>begin<br>wj:=opendialog1.filename;<br>end;<br>setlength(s,1000);<br>assignfile(f,wj);<br>reset(f);<br>while not seekeof(f) do<br>begin<br>if seekeoln(f) then<br>readln;<br>read(f,s);<br>edit1.text:=s;<br>end;<br>closefile(f);<br>except<br>end;
 
不知道你这么用是什么意思?如果是一些EDIT的初始化的话,建议使用INI文件。
 
呵,我来改一下教父的代码吧!<br>procedure readtxt(targfilename:string;i:integer);//I为你所要读的行数<br>sl:=TStringList.Create;<br>try<br>&nbsp; sl.LoadFromFile(targfilename);<br>&nbsp; if i&lt;=sl.count-1 then<br>&nbsp; &nbsp; Edit1.Text:=sl.strings[i-1]<br>&nbsp; else<br>&nbsp; &nbsp; showmessage('所需要的行数超过实际行数!')<br>finally<br>&nbsp; sl.Free;<br>end;<br>我个人也倾向用STRINGLIST
 
后退
顶部