有两个文件,"1.txt"和"2.txt"内容分别为
1.txt:
3
4
52.txt:
1
2
3
4现要求用程序把"2.txt"中有,而"1.txt"中没有的数据进行筛选,并将其写入"3.txt",我写的程序为
StreamReader sr1 = new StreamReader(@"d:\1\1.txt", Encoding.GetEncoding("gb2312"));
            StreamReader sr2 = new StreamReader(@"d:\1\2.txt", Encoding.GetEncoding("gb2312"));
            string line1;
            string line2;
            while ((line2 = sr2.ReadLine()) != null)
            {
                while ((line1 = sr1.ReadLine()) != null)
                {
                    if (!line1 .Contains (line2 ))
                    {
                        StreamWriter sw = new StreamWriter(@"d:\1\3.txt", true, Encoding.GetEncoding("gb2312"));
                        sw.WriteLine(line2);
                        sw.Close();
                    }
                }
            }
正确的结果应为:

2可程序运行结果却为:
1
1
1请高手帮忙看看错在哪,如何改

解决方案 »

  1.   

    ReadLine 只是读取一行,1.txt和2.txt中都有换行,3.txt中当然只能是那样啦~
      

  2.   

    全部读到2个数组,再去查找
      
    *****************************************************************************
    欢迎使用CSDN论坛专用阅读器 : CSDN Reader(附全部源代码) 
    http://feiyun0112.cnblogs.com/
      

  3.   

    是只读取一行,我的意思是,每次读取一行,读了之后马上比较,然后接着读下行,直到读完或者说是否我不应用ReadLine ?
      

  4.   

    如果文件不大,就分别读到一个Dictionary里面然后再比较出结果最后放入3.txt,如果文件比较大的话,可以用如下的方式:using (StreamReader sr2 = new StreamReader(@"d:\1\2.txt", Encoding.GetEncoding("gb2312")))
            {
                string line1;
                string line2;
                while (sr2.EndOfStream != false)
                {
                    line2 = sr2.ReadLine();
                    bool contains = false;
                    using (StreamReader sr1 = new StreamReader(@"d:\1\1.txt", Encoding.GetEncoding("gb2312")))
                    {
                        while (sr1.EndOfStream != false)
                        {
                            line1 = sr1.ReadLine();
                            if (line1.Contains(line2))
                            {
                                contains = true;
                                break;
                            }
                        }
                    }
                    if (!contains)
                    {
                        using (StreamWriter sw = new StreamWriter(@"d:\1\3.txt", true, Encoding.GetEncoding("gb2312")))
                        {
                            sw.WriteLine(line2);
                        }
                    }
                }
            }
      

  5.   

    将1.txt中的数据读到一个List<string> values1,
    然后:
      StreamWriter sw = new StreamWriter(@"d:\1\3.txt", true, Encoding.GetEncoding("gb2312")); 
      while ((line1 = sr2.ReadLine()) != null) 
      { 
           if (!values1.Contains (line1)) 
           { 
               sw.WriteLine(line1); 
           } 
       } 
       sw.Close(); 
      

  6.   

    问题出现在这里:while ((line1 = sr1.ReadLine()) != null) ,当sr2读第一行的时候,sr1就读1.txt结束了,当sr2读第二行的时候,((line1 = sr1.ReadLine())就等于null了,因为他读文件已经结束了。
      

  7.   

    读到ArrayList中比较。
    你的程序是有问题的。
    2.txt中每一行都会和1中每行比较,如果和1中任何一行不一样,你都会写到3中,当然会出现你说的那样的结果了。
      

  8.   

    修改了你的程式,試試看符不符合你的需求。
            string line1;
            string line2;        StreamReader sr2 = new StreamReader(@"d:\2.txt", Encoding.GetEncoding("gb2312"));
            while ((line2 = sr2.ReadLine()) != null)
            {
                int isSame = 0, noSame = 0;            StreamReader sr1 = new StreamReader(@"d:\1.txt", Encoding.GetEncoding("gb2312"));
                while ((line1 = sr1.ReadLine()) != null)
                {
                    if (!line1.Contains(line2))
                        noSame = 1; //沒找到相同的旗標
                    if (line1.Contains(line2))
                        isSame = 1; //有找到相同的旗標
                }
                if (noSame == 1 && isSame == 0) //表示找不到相同的就做下面的處理
                {
                    StreamWriter sw = new StreamWriter(@"d:\3.txt", true, Encoding.GetEncoding("gb2312"));
                    sw.WriteLine(line2);
                    sw.Close();
                }
            } 
      

  9.   

    using System.Collections;
      StreamReader sr1 = new StreamReader(@"d:\1\1.txt", Encoding.GetEncoding("gb2312"));
                StreamReader sr2 = new StreamReader(@"d:\1\2.txt", Encoding.GetEncoding("gb2312"));
                string line1;
                string line2;
                List<string> s = new List<string>();
                List<string> sarray = new List<string>();
                while ((line2 = sr2.ReadLine()) != null)
                {
                    s.Add(line2);               
                }
                while ((line1 = sr1.ReadLine()) != null)
                {
                    sarray.Add(line1);               
                }
                StreamWriter sw = new StreamWriter(@"d:\1\3.txt", true, Encoding.GetEncoding("gb2312"));                         
                foreach (string var in s)
                {
                    if (sarray.Contains(var))
                    {
                        sw.WriteLine(var);
                    }
                }
                sw.Close();