一个文本文件(*.txt,或 *.rtf),里面内容以#号分割,如:
1#11:00#sdfsdf
......
1000#2:54#得额为现在想把每个字段读出,赋给变量,如:var
id:integer;
timenow:Ttime;
descr:string;
begin
while (判断文件是否结束=没结束)do
begin
//读文件中的一行;
//赋给三个变量;
//读下一行;
end;
.......
end;
请问该怎么写,谢谢
1#11:00#sdfsdf
......
1000#2:54#得额为现在想把每个字段读出,赋给变量,如:var
id:integer;
timenow:Ttime;
descr:string;
begin
while (判断文件是否结束=没结束)do
begin
//读文件中的一行;
//赋给三个变量;
//读下一行;
end;
.......
end;
请问该怎么写,谢谢
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;
CreateFile(); FileSeek(); FileClose()
F:TextFile;
begin
AssignFile(F,'./Test.txt');
Reset(F);
while not Eof(F) do
begin
ReadLn(F,ALine);
end;
CloseFile(F);
end;
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;
你可以这样来写: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;
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;
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;