Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7591711
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T20:43:06+00:00 2026-05-30T20:43:06+00:00

The purpose of the code is to find a portion of a string (from

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T20:43:08+00:00Added an answer on May 30, 2026 at 8:43 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a line of code like this: List<string>[] apples = new List<string>()[2]; Its
The code in question is here: var $item = $(this).parent().parent().find('input'); What is the purpose
I've got following (simplified for example purpose) code and it works: void log(const string
The purpose of this code is to pull upgrade.zip from a central server, extract
Quick question, What have I done wrong here. The purpose of this code is
Suppose i have the code snippet as follows : ( clarification purpose/not well formed
One recommends me the following code apparently only in .zshrc without explaining its purpose
I have the following code in a class of mine. The purpose of this
I'm trying to find out if two strings I have are the same, for
I have the following code which is an implementation of BPM (bipartite matching, from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.