社区
Delphi
帖子详情
请问如何读取文本文件里的数据
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;
请问该怎么写,谢谢
...全文
60
15
打赏
收藏
请问如何读取文本文件里的数据
一个文本文件(*.txt,或 *.rtf),里面内容以#号分割,如: 1#11:00#sdfsdf ...... 1000#2:54#得额为 现在想把每个字段读出,赋给变量,如: var id:integer; timenow:Ttime; descr:string; begin while (判断文件是否结束=没结束)do begin //读文件中的一行; //赋给三个变量; //读下一行; end; ....... end; 请问该怎么写,谢谢
复制链接
扫一扫
分享
转发到动态
举报
AI
作业
写回复
配置赞助广告
用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打开阿,然后一列一列赋值阿
Android
读取
文本文件
的demo
一个Android
读取
文本文件
的demo,txt文件放到res/raw文件夹下,通过代码将其
读取
并显示在屏幕上。
python
读取
文本文件
-python
读取
文本文件
数据
本文要点刚要:(一)读
文本文件
格式的
数据
函数:read_csv,read_table1.读不同分隔符的
文本文件
,用参数sep2.读无字段名(表头)的
文本文件
,用参数names3.为
文本文件
制定索引,用index_col4.跳行
读取
文本文件
,用skiprows5.
数据
太大时需要逐块
读取
文本
数据
用chunksize进行分块。(二)将
数据
写成
文本文件
格式函数:to_csv范例如下:(一)
读取
文本文件
格...
python
读取
txt
数据
-python
读取
文本文件
数据
本文要点刚要:(一)读
文本文件
格式的
数据
函数:read_csv,read_table1.读不同分隔符的
文本文件
,用参数sep2.读无字段名(表头)的
文本文件
,用参数names3.为
文本文件
制定索引,用index_col4.跳行
读取
文本文件
,用skiprows5.
数据
太大时需要逐块
读取
文本
数据
用chunksize进行分块。(二)将
数据
写成
文本文件
格式函数:to_csv范例如下:(一)
读取
文本文件
格...
C++
读取
文本文件
的几种方法
前几天要用到C++
读取
文本文件
,就学习了一下几种不同的
读取
方法:
文本文件
内容如下: 第一种:直接
读取
,以空格换行 int main() { ifstream infile; infile.open("qqzl.txt", ios::in); if (!infile.is_open()) { cout << "
读取
文件失败" << endl; retu...
C# 逐行
读取
文本文件
本文转载自C# 逐行
读取
文本文件
目录使用 C# 中的 File.ReadLines() 方法逐行
读取
文本文件
使用 C# 中的File.ReadAllLines() 方法逐行
读取
文本文件
使用 C# 中的 StreamReader.ReadLine() 方法逐行
读取
文本文件
我们可以对
文本文件
执行多种操作。要在任何程序中使用文件中的
数据
,我们首先需要以适当的
数据
结构
读取
该
数据
。 在 C# 中,有几种有效地逐行
读取
文本文件
的方法。 使用 C# 中的 File.ReadLines() 方法逐行
读取
文本文件
Fil
Delphi
5,927
社区成员
262,931
社区内容
发帖
与我相关
我的任务
Delphi
Delphi 开发及应用
复制链接
扫一扫
分享
社区描述
Delphi 开发及应用
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章