内部While循环仅运行一次 [英] Inner While Loop only run once

查看:93
本文介绍了内部While循环仅运行一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该代码的目的是在一长串字符串(另一个文档)中找到一个字符串的一部分(来自一个文档),然后将结果返回到列表中.

The purpose of the code is to find a portion of a string (from one document) in a long list of strings (another document), and return the results to a list.

我有两个while循环,一个嵌套在另一个循环中.外循环读取该文档的每一行,而内循环读取其文档的每一循环.由于某种原因,内部while只运行一次(对于外部循环的第一次迭代).您能解释为什么以及如何解决吗?似乎它只读取所有行一次,然后不再读取它们...我需要内部循环才能在每次外部循环运行时逐行读取.

I have two while loops, one nested in the other. The outer loop reads each line of that document, and the inner reads each loop of its document. For some reason, the inner while only runs once (for the outer loop's first iteration). Can you explain why and how I could fix it? It seems like it only reads all the lines once and then doesn't read them again... I need the inner loop to read line by line every time the outer loop runs.

这是我的代码.

    private static void getGUIDAndType()
    {
        try
        {
            Console.WriteLine("Begin.");

            String dbFilePath = @"C:\WindowsApps\CRM\crm_interface\data\";
            StreamReader dbsr = new StreamReader(dbFilePath + "newdbcontents.txt");
            List<string> dblines = new List<string>();

            String newDataPath = @"C:\WindowsApps\CRM\crm_interface\data\";
            StreamReader nsr = new StreamReader(newDataPath + "HolidayList1.txt");
            List<string> new1 = new List<string>();

            string dbline;
            string newline;

            Program prog = new Program();

            while ((dbline = dbsr.ReadLine()) != null)
            {
                while ((newline = nsr.ReadLine()) != null)
                {
                    newline = newline.Trim();
                    if (dbline.IndexOf(newline) != -1)
                    {//if found... get all info for now
                        Console.WriteLine("FOUND: " + newline);
                        System.Threading.Thread.Sleep(1000);
                        new1.Add(newline);
                    }
                    else
                    {//the first line of db does not contain this line... 
                        //go to next newline. 
                        Console.WriteLine("Lines do not match - continuing");
                        continue;
                    }
                }
                nsr.Close();
                Console.WriteLine("should be run as many times as there are dblines");
                Console.WriteLine(newline);
                System.Threading.Thread.Sleep(5000);
                //continue;
            }

            Console.WriteLine("Writing to dbc2.txt");
            System.IO.File.WriteAllLines(@"C:\WindowsApps\CRM\crm_interface\data\dbc2.txt", new1.ToArray());
            Console.WriteLine("Finished. Press ENTER to continue.");

            Console.WriteLine("End.");
            Console.ReadLine();
        }
        catch (Exception ex)
        {
            Console.WriteLine("Error:  " + ex);
            Console.ReadLine();
        }
    }

我尝试将内部循环设置为方法,但是当我在方法中引用dbline时,在此行得到了对象引用异常: if (dbline.IndexOf(newline) != -1).并没有花费太多时间来解决这个问题,而是回到了嵌套循环中.但是如果我采用这种方法,我认为我的结果不会有任何不同.

I tried setting the inner loop as a method, but when I refer to dbline in the method I get the object reference exception at this line: if (dbline.IndexOf(newline) != -1). Didn't really spend much time trying to fix that and went back to the nested loops; but if I employed this method I don't think my result would be any different.

如果您能想到一种更好的方式来做我想做的事情,请告诉我.我不能使用contains,因为我引用的两个txt文档中的行并不完全相同.

If you can think of a better way to do what I'm trying to, please let me know. I can't use contains because the lines in the two txt documents I'm referencing are not exactly the same.

谢谢.

推荐答案

对于外循环的每次迭代,都需要将内循环的StreamReader倒带到文件的开头:

For every iteration of the outer loop, you need to rewind the StreamReader of the inner loop to the beginning of the file:

  while ((dbline = dbsr.ReadLine()) != null)
  {
     // Reset
     nsr.BaseStream.Position = 0;
     nsr.DiscardBufferedData(); 

     while ((newline = nsr.ReadLine()) != null)
     {
       .....
     }
     nsr.Close(); // remove that


根据评论进行


EDIT according to comment:

您还需要在内循环之后删除StreamReader的Close(),将其移到外循环之后.

You also need to remove that Close() of the StreamReader after the inner loop, move it after the outer loop.

这篇关于内部While循环仅运行一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆