问个关于读取txt文件的问题

xbingle 2008-04-23 11:37:47
现在又一个txt文档,内容都是数字,比如有一行:1,2,3,4,5,想问如何能一次取出1 ,2 ,3 ,4 ,5 这几个数字来
...全文
90 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
xbingle 2008-04-23
  • 打赏
  • 举报
回复
原来是我写错了,是依次取出,不是一次取出
xbingle 2008-04-23
  • 打赏
  • 举报
回复
以上代码都是读一行的吧,我想读取一行中的某些字符呢?
Temp_DX 2008-04-23
  • 打赏
  • 举报
回复
按楼上的做吧,拿数字当字符串取
wxmwxmwxm 2008-04-23
  • 打赏
  • 举报
回复
1楼全部取了
liangpei2008 2008-04-23
  • 打赏
  • 举报
回复

var
F:TextFile;
s: string;
begin
AssignFile(F,'c:\Test.txt');
Reset(F); //只读打开
Readln(F, s); //读取
ShowMessage(s); //显示: 第一行
CloseFile(F);
end;
liangpei2008 2008-04-23
  • 打赏
  • 举报
回复

var
Str:TStrings;
begin
Str:=TStringList.Create;
try
Str.LoadFromFile('c:\Test.txt');
Showmessage(Str[0]);//0行
finally
Str.Free;
end;
end;
blazingfire 2008-04-23
  • 打赏
  • 举报
回复

var
List: TStrings;
i, iPos: Integer;
S, OnePart{一个部分}: String;
begin
List := TStringList.Create;
try
List.LoadFromFile('文本文件名称');
for i := 0 to List.Count - 1 do
begin
S := List[i];//1,2,3,4
while S <> '' do
begin
iPos := Pos(',', S);//查找","的位置
if iPos > 0 then
begin
OnePart := Trim(Copy(S, 1, iPos - 1)); //","的位置前的部分
Delete(S, 1, iPos);
end
else
begin
OnePart := Trim(S);
S := '';//已经取完所有的数据
end;

ShowMessage(OnePart);//显示出结果,StrToInt(OnePart)就是数字了
end;
end;
finally
List.Free;
end;
end;
Temp_DX 2008-04-23
  • 打赏
  • 举报
回复
读到s中以后

a:array of integer;
i:=1;
j:=i;
k:=1;
while i<=length(str) do
begin
while str[j]in['0'..'9','-','.'] do inc(j);
if j<>i then
begin
a[k]:=strtointdef(copy(str,i,j-1),maxint); //a[k]=maxint 为错误
inc(k);
end;
i:=j+1;
j:=i;
end;

16,749

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 语言基础/算法/系统设计
社区管理员
  • 语言基础/算法/系统设计社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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