Inner While Loop only run once

走远了吗. 提交于 2019-12-12 03:14:49

问题


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.

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.

Here is my code.

    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();
        }
    }

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.

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.

Thanks.


回答1:


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:

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




回答2:


Your inner loop consumes the file. Seek back to the beginning.




回答3:


Since your while loop is terminating without a break or return statement, it means this loop condition is evaluating to false:

(newline = nsr.ReadLine()) != null

That is, nsr.Readline() is returning null because you have hit end-of-file.

If you actually intend to read lines multiple times from nsr, you can seek to the beginning by resetting position on the underlying Stream and discarding the StreamReader's buffer:

nsr.BaseStream.Position = 0;
nsr.DiscardBufferedData();


来源:https://stackoverflow.com/questions/9487623/inner-while-loop-only-run-once

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!