请问如何读取文本文件里的数据

amartapple 2003-05-22 12:47:55
一个文本文件(*.txt,或 *.rtf),里面内容以#号分割,如:
1#11:00#sdfsdf
......
1000#2:54#得额为

现在想把每个字段读出,赋给变量,如:

var
id:integer;
timenow:Ttime;
descr:string;
begin
while (判断文件是否结束=没结束)do
begin
//读文件中的一行;
//赋给三个变量;
//读下一行;
end;
.......
end;


请问该怎么写,谢谢

...全文
51 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
heaven765 2003-08-22
  • 打赏
  • 举报
回复
我是受益者
谢谢各位了
amartapple 2003-05-24
  • 打赏
  • 举报
回复
上面的方法虽然可以用#断字,但是空格也可以呀,不知道怎么能解决
nitxht 2003-05-22
  • 打赏
  • 举报
回复
up
lion_lh 2003-05-22
  • 打赏
  • 举报
回复
简单点
var
tt,ts:tstringlist;
i,j:integer;
begin
tt:=tstringlist.create;
ts:=tstringlist.create;
tt.LoadFromFile('m:\xx.txt');
ts.Delimiter:='#';

for i:=0 to tt.count-1 do
begin
ts.DelimitedText:=tt[i];
for j:=0 to ts.Count-1 do
showmessage(ts[j]);
end;
tt.free;
ts.Free;
end;
risingsoft 2003-05-22
  • 打赏
  • 举报
回复
楼上的代码好多啊 呵呵~~~~~ 楼主记得给分啊~~~~
risingsoft 2003-05-22
  • 打赏
  • 举报
回复
抱歉,回复早了,把其他的注释加上:

procedure TForm1.Button1Click(Sender: TObject);
var
txtF: TextFile; //文本文件
tmpS, tmpL: String; //各个字段字符串值,各行字符串值
lenL, posS, filP: Integer; //各行字符串长度,#字符位置,字段索引
begin
AssignFile(txtF,'demo.txt'); //文本文件关联
Reset(txtF); //打开文本文件
filP := 1; //初始化字段序号为1
While not Eof(txtF) do //循环读取各行文本
begin
ReadLn(txtF,tmpL); //读取一行字符串
while posS <> 0 do //循环处理各行字符串
begin
tmpL := Trim(tmpL); //去掉前后空格
lenL := Length(tmpL); //获得各行字符串长度
posS := Pos('#',tmpL); //获得#符号位置
tmpS := Copy(tmpL,1,posS); //获得各个字段值
tmpL := Copy(tmpL,posS+1,lenL-posS); //去掉已经提取的字段字符串
Table1.Fields[filP].AsString := tmpS; //给表字段赋值
Inc(filP); //更改字段索引
if filP > 3 then filP := 1; //字段索引循环,1-3
end;
end;
CloseFile(txtF); //关闭文本文件
end;
JMing 2003-05-22
  • 打赏
  • 举报
回复

你可以这样来写:

var
id:integer;
timenow:Ttime;
descr:string;
f:Textfile; //数据文件
ch:char
strlen:integer; //一行文字的长度
t1,t2,t3:string; //三个变量
begin
asssignfile(f,'文件路径和名称');
reset(f);

while not f.eof do
begin
//读文件中的一行;
readln(f,descr);
strlen := length(descr); //获取一行信息的长度
t1 := '';
t2 := '';
t3 := '';

id := 1;
ch := descr[id];
//读取第一个变量的值
while ch<>'#' do
begin
t1 := t1 + ch;
inc(id); //id 加一
ch := descr[id];
end;
//读取第二个变量的值
inc(id);
ch := descr[id];
while ch<>'#' do
begin
t2 := t2 + ch;
inc(id);
ch := descr[id];
end;
//读取第三个变量的值
inc(id);
ch := descr[id]
while id <= strlen do
begin
t3 := t3 + ch;
inc(id);
ch := descr[id];
end;
//处理取出来得三个变量
end;
.......
end;
risingsoft 2003-05-22
  • 打赏
  • 举报
回复
procedure TForm1.Button1Click(Sender: TObject);
var
txtF: TextFile; //文本文件
tmpS, tmpL: String; //各个字段字符串值,各行字符串值
lenL, posS, filP: Integer; //各行字符串长度,#字符位置,字段索引
begin
AssignFile(txtF,'demo.txt'); //文本文件关联
Reset(txtF); //打开文本文件
filP := 1;
While not Eof(txtF) do
begin
ReadLn(txtF,tmpL);
while posS <> 0 do
begin
tmpL := Trim(tmpL);
lenL := Length(tmpL);
posS := Pos('#',tmpL);
tmpS := Copy(tmpL,1,posS);
Table1.Fields[filP].AsString := tmpS;
Inc(filP);
if filP > 3 then filP := 1;
end;
end;
CloseFile(txtF);
end;
boboiilee19 2003-05-22
  • 打赏
  • 举报
回复
var
F:TextFile;
begin
AssignFile(F,'./Test.txt');
Reset(F);
while not Eof(F) do
begin
ReadLn(F,ALine);
end;
CloseFile(F);
end;
amartapple 2003-05-22
  • 打赏
  • 举报
回复
谢谢,可是:

这样只能读出文件的一行,那么怎么才能根据#来断行取字
zhyongjava 2003-05-22
  • 打赏
  • 举报
回复
GZ
foilsman 2003-05-22
  • 打赏
  • 举报
回复
或者用
CreateFile(); FileSeek(); FileClose()
foilsman 2003-05-22
  • 打赏
  • 举报
回复
var
f:textfile;
str:string;
begin
assignfile(f,'a.txt');
reset(f);
while not eof(f) do
begin
readln(f, str);
showmessage(str);
end;
closefile(f);
end;
amartapple 2003-05-22
  • 打赏
  • 举报
回复
如何实现;),意思还算明白,可是不会实现
anan3310 2003-05-22
  • 打赏
  • 举报
回复
把文本写到TSTRINGLIST然后一行一行做判断,或者可以把它保存为CVS的文件这个可以用EXCEL打开阿,然后一列一列赋值阿

5,388

社区成员

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

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