Tommy Sin

Tommy Sin

  • NA
  • 19
  • 104.2k

Regarding File IO for C#.

Jan 17 2011 1:38 PM

I am not sure why my code does not work.
 
I have one version for forloop and another one for while loop,
However, the output that is saved in "Discrepancy1.txt" appears to be wrong after the compilation.
Do you have any clue?
 
 
(For loop version)
TextReader
Lbloomberg = new StreamReader("Lbloomberg.txt");
TextReader Reuters = new StreamReader("Reuters.txt");
TextWriter Discrepancy1 = new StreamWriter("Discrepancy1.txt");
string Lbloomberg_item;
string Reuters_item ;
//TextWriter Discrepancy2 = new StreamWriter("Discrepancy2.txt");
 
 

for (int num_Lbloomberg = 0; num_Lbloomberg < 2000; num_Lbloomberg++) //For the data that Lbloomberg has but not Reuters
{
   Lbloomberg_item = Lbloomberg.ReadLine();
   bool a = false;
   for (int num_Reuters = 0; num_Reuters < 5000; num_Reuters++)
   {
      Reuters_item = Reuters.ReadLine();
      if (Lbloomberg_item == Reuters_item)
      {
         a =
true;
      }
   }
   if (a == true)
   {
      Discrepancy1.WriteLine(Lbloomberg_item);
   }
 }


Lbloomberg.Close();
Reuters.Close();

return;
}
 
 
 
 
 
(While Loop Version)


while ((Lbloomberg_item = Lbloomberg.ReadLine()) != null)//break right after searching UBAN . (UBAN .1)
{
   bool a = false;
   while ((Reuters_item = Reuters.ReadLine()) != null)//break right after searching (CTC .1)
   {
      if (Lbloomberg_item == Reuters_item)
      {
         a = true;
      }
   }
   if (a == true)
   {
      Discrepancy1.WriteLine(Lbloomberg_item);
   }
}

Lbloomberg.Close();
Reuters.Close();

Answers (2)