请高手帮忙看一下这段代码.(95分)

J

joann

Unregistered / Unconfirmed
GUEST, unregistred user!
文本文件如下:
会计科目: 0001
===================================================================
客户名称 贷款帐号 合同号
-------------------------------------------------------------------
华小二 103035500100000079 019905500001600
何瑟 103035500100000078 019905500000500
...................
曹夹英 103035500100000176 019905500000900
-------------------------------------------------------------------
本页笔数: 45 本页金额: 2,950,033.15
-------------------------------------------------------------------



会计科目: 0002
===================================================================
客户名称 贷款帐号 合同号
-------------------------------------------------------------------
贺晓 103035500100000021 019905500001901
倪任尧 103035500100000004 019905500000101
..................
郑何方 103035500100000068 019905500000201
-------------------------------------------------------------------
本页笔数: 38 本页金额: 22,504,728.81
-------------------------------------------------------------------



会计科目: 0003
===================================================================
客户名称 贷款帐号 合同号
-------------------------------------------------------------------
李茜 103035500100000123 019905500001100
...................
万森林 103035500100000254 019905500001200
林国美 103035500100000085 019905500001300
-------------------------------------------------------------------
本页笔数: 27 本页金额: 22,504,728.81
-------------------------------------------------------------------



会计科目: 0002
===================================================================
客户名称 贷款帐号 合同号
-------------------------------------------------------------------
张望人 103035500100000023 019905500002800
何其一 103035500100000034 019905500004500
...................
赵达标 103035500100000045 019905500003500
-------------------------------------------------------------------
本页笔数: 46 本页金额: 22,504,728.81
-------------------------------------------------------------------

取出" 会计科目: 0002 " 中的数据而不是全部,另存temp.txt如下:
贺晓 103035500100000021 019905500001901
倪任尧 103035500100000004 019905500000101
..................
郑何方 103035500100000068 019905500000201
张望人 103035500100000023 019905500002800
何其一 103035500100000034 019905500004500
...................
赵达标 103035500100000045 019905500003500

如何写?
有好心网友这样写
procedure FormatTextFile;//格式化文本文件
var
f,f0:tstringlist;
i,j:integer;
b:boolean;
begin
f:=tstringlist.create;
f0:=tstringlist.create;
f.loadfromfile('文本文件.txt');
j:=0;
b:=false;
for i:=0 to f.count-1 do
begin
if b then
begin
if not(f[1] in ['-','=',' ']) then
f0.add(f)
else
begin
if f[1]='-' then inc(j);
if j>2 then break;
end;
end;
if pos('会计科目: 0002',f)>0 then b:=true;//要注意':'与'0002'中间的空格数目
end;
f0.savetofile('temp.txt');
f.free;
f0.free;
end;
运行通过,但读到文本'temp.txt'中的内容为空.
请求帮助.

 
其它你调试一下,
数据是否加到f0,
单步跟踪
 
我认为我的代码应该 正确,上面的代码可能是
if pos('会计科目: 0002',f)>0 then b:=true;//要注意':'与'0002'中间的空格数目
这句话有问题,因为可能每次都等于0所以找不到,b始终是false,就算找到了,一旦b为true
就不会再为false,所以也会出错的。
//////////////////////////////////////////////////应该这样写
if pos('会计科目: 0002',f)>0 then b:=true
else b:=false;
//////////////////////////////////////////////////
我的方法是找一个没有空格的串,这样应该好些
procedure FormatTextFile;//格式化文本文件
var
File1,File2:TextFile;
Strtemp:String;
begin
AssignFile(File1,'文本文件.txt');
reset(File1);
AssignFile(File2,'temp.txt');
if FileExists('temp.txt') then Append(File2)
else Rewrite(File2);
while not Eof(File1) do
begin
readln(File1,StrTemp);
if Pos('103035500',StrTemp)<>0 then
begin
Writeln(File2,StrTemp);
end;
end;
CloseFile(File2);
CloseFile(File1);

end;
 
接受答案了.
 
顶部