有如下的数据
b4070.dat b4190.dat OK 0
b4080.dat b4090.dat OK 0.2
如何从文件中把这些数据读出来,并放到不同的变量中?如把0.2放到fDat变量中。
能否有示例代码。立即给分
b4070.dat b4190.dat OK 0
b4080.dat b4090.dat OK 0.2
如何从文件中把这些数据读出来,并放到不同的变量中?如把0.2放到fDat变量中。
能否有示例代码。立即给分
我想了一个办法:
先把文件读到一个TStrings的对象中
然后用AnsiReplaceStr函数,把TStrings.Text中的所有空格(' ')换成回车符(#13#10)
然后逐行读其中的内容就可以了.
stringlist1.loadformfile('filename');
s1:=s.Strings[0];
fDat:=trim(copy(s1,pos('OK',s1)+2,s1-pos('OK',s1)+2));
FileName : TextFile;
Str,Tmp : String;
TStrList : TStringList;
i,k : Integer;
begin
TStrList := TStringList.Create;
TStrList.Clear;
if not (FileExists(GetNameDir(Application.ExeName)+'xuanhao.txt')) then
begin
Application.MessageBox('xuanhao.txt文件不存在,请先转出TXT文件','提示',MB_OK+MB_ICONSTOP);
Exit;
end;
AssignFile(FileName,GetNameDir(Application.ExeName) + 'xuanhao.txt');
Reset(FileName);
Readln(FileName,Str);
CloseFile(FileName);
if Str <> '' then
begin
i := pos(',',str);
while i > 0 do
begin
Tmp := copy(Str,1,i - 1);
TStrList.Add(Tmp);
Str := copy(Str,i + 1,length(Str)-i);
i := pos(',',Str);
end;
if Str <> '' then
begin
TStrList.Add(Str);
end;
end;