文本文件如下:
会计科目:  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[i][1] in ['-','=',' ']) then  
           f0.add(f[i])
       else
       begin
          if f[i][1]='-' then inc(j);
          if j>2 then break;
       end; 
    end;  
    if pos('会计科目:  0002',f[i])>0 then b:=true;//要注意':'与'0002'中间的空格数目
end;
f0.savetofile('temp.txt');
f.free;
f0.free;
end;
运行通过,但读到文本'temp.txt'中的内容为空.
请求帮助.

解决方案 »

  1.   

    procedure TForm1.Button2Click(Sender: TObject);
    var
      MyTextFile, MyTextFile1: TextFile;
      S: string[255];
    begin
      AssignFile(MyTextFile, '文本文件.txt');
      assignFile(MyTextFile1,'tmp.txt');
      Reset(MyTextFile);
      Rewrite(MyTextFile1);
      try
        while not Eof(MyTextFile) do
        begin
          Readln(MyTextFile, S);
          if Pos('会计科目:  0002', s) > 0 then
          begin
            Readln(MyTextFile, S);
            Readln(MyTextFile, S);
            Readln(MyTextFile, S);  
            while not Eof(MyTextFile) do
            begin
              Readln(MyTextFile, S);
              if Pos('-', s) > 0 then
                break
              else
                WriteLn(MyTextFile1, s);
            end;
          end;
        end;
      finally
        CloseFile(MyTextFile);
        CloseFile(MyTextFile1);
        Caption := 'ok';
      end;
    end;
      

  2.   

    if not(f[i][1] in ['-','=',' ']) then f0.add(f[i])这一句有问题, 有效行还不是有空格,肯定是空了。建议查数字个数,超过12位数的肯定是。