Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1227681
  • 博文数量: 788
  • 博客积分: 4000
  • 博客等级: 上校
  • 技术积分: 7005
  • 用 户 组: 普通用户
  • 注册时间: 2008-08-19 15:52
文章存档

2017年(81)

2011年(1)

2009年(369)

2008年(337)

分类:

2009-04-22 11:00:58



有两个文本文件  
  文件1内容  
  444  
  333  
  222  
  111  
  文件2内容  
  111  
  222  
  我想在文件1中每读出一行再与文件2中的每一行比较,如果相同,就不往下比了,如果读到文件2的最后还是没有相同的,就把文件1的这一行写入文本文件3中,然后再重复,直到文件1读完,我写的代码如下(循环语句部分),调试后没有这个效果,请高人指点、改正,谢谢了  
  While   Not   Eof(F1)   Do  
  begin  
          Readln(F1,Str1);  
          Flage:=true;  
          While   (Not   Eof(F2))   And   Flage=true   Do  
          begin  
                  Readln(F2,Str2);  
                  If   CompareText(str1,str2)=0   Then  
                          begin  
                          Flage:=false;  
                          end;  
          END;  
          If   Flage=true   Then  
                  begin  
                  Writeln(F3,Str1);  
                  end;  
  End;  
 

下面的代码我是用了三个StringList,可以实现你的功能.注意,C:\下要存在1.txt和2.txt,或者你自己改路径也可以.  
   
  var  
      Index1,index2,index3:integer;  
      StrList1,StrList2,StrList3:Tstringlist;  
      found:boolean;  
  begin  
            strlist1:=Tstringlist.Create;  
            strlist2:=Tstringlist.Create;  
            strList3:=TstringList.Create;  
            strlist1.LoadFromFile('c:\1.txt');  
            strlist2.LoadFromFile('c:\2.txt');  
            for   index1:=0   to   strlist1.Count-1   do  
                    begin  
                    for   index2:=0   to   strlist2.Count-1   do  
                            begin  
                            if   strlist2[index2]=strlist1[index1]   then  
                                  begin  
                                  found:=true;  
                                  break;  
                                  end;  
                            end;  
                    if   not   found   then   strlist3.Add(strlist1[index1]);  
                    found:=false;  
                    end;  
            strlist3.SaveToFile('c:\3.txt');  
            strlist3.Free;  
            strlist2.Free;  
            strlist1.Free;  
  end;

忘记在第一个for循环前面给found赋初值了,你自己加上吧:  
  found:=false;  
  或者在第一层for循环开始时赋初值,这样就不需要22行的found:=false了

谢谢lihuasoft的回复,谢谢你给出的代码,我会好好的学习的,如果能指出我的错误,那就太好了,我也就知道自己错在哪里了,这样也是对自己的提高!

以下是你的代码:     注意带注释的那句是我添加的  
   
  While   Not   Eof(F1)   Do  
  begin  
          Readln(F1,Str1);  
          Flage:=true;  
          Reset(F2);//把重置F2的语句reset(f2)放在这里,因为在一次循环之后,游标已到底  
          While   (Not   Eof(F2))   And   Flage=true   Do  
          begin  
                  Readln(F2,Str2);  
                  If   CompareText(str1,str2)=0   Then  
                          begin  
                          Flage:=false;  
                          end;  
          END;  
          If   Flage=true   Then  
                  begin  
                  Writeln(F3,Str1);  
                  end;  
  End;  
  我没有测试,你自己看一下是不是这个原因

太好了,没有错,我调试了,谢谢你lihuasoft老兄,看来我还得好好学习!



--------------------------
新闻:大脑推客:通过电脑思考即可发布Twitter消息
网站导航: 博客园首页  新闻  .NET频道  社区  博问  闪存  找找看
阅读(962) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~