hi everyone, im having a hard time with my loop, what i want to do is to print f1, f2, f2,f2... till it finishes to read, then f1, f2, f2... , whats happening to my output is f1,f2, f2..., then f1, f1 till end, i want to ask, how will i reset my sr2.EndOfStream so it will satisfy my second while condition, please help... thank you...

try
  {                         
    string fileName = @"C:\txt1.Text
    string fileName2 = @"C:\txt1.Text
    StreamReader sr = new StreamReader(fileName);
    StreamReader sr2 = new StreamReader(fileName2);

    string x, x2;

      using (sr)
      {
         StreamWriter SW = File.CreateText("c:\\MyTextFile.txt");

         while (!sr.EndOfStream)
         {
           x = sr.ReadLine();
           if (x.Trim() != "")
           {
             SW.Write("f1 " + x.Substring(11, 2));  
             SW.Write(x.Substring(13, 2));  
             SW.Write(x.Substring(15, 3));  
             SW.Write(x.Substring(18, 3));  
             SW.Write(x.Substring(26, 4));  
             SW.Write(x.Substring(30, 4));  
             SW.WriteLine("");
           }
           while (!sr2.EndOfStream)
           {
             x2 = sr2.ReadLine();
             if (x2.Trim() != "")
             {
               SW.Write("f2 " + x2.Substring(11, 2)); 
               SW.Write(x2.Substring(13, 2));
               SW.Write(x2.Substring(15, 3));
               SW.Write(x2.Substring(18, 3));
               SW.Write(x2.Substring(26, 4));
               SW.Write(x2.Substring(30, 4));
               SW.WriteLine("");
              }
           }

/// how can i reset sr2.EndOfStream here, so when the first while loop 
/// increment it will satisfy the second loop?

       }                   
        SW.Close();
        System.Diagnostics.Process.Start(@"C:\MyTextFile.txt");
                   
        sr.Close();
        sr2.Close();
   }
 }
catch(Exception ex)
 {
   MessageBox.Show(ex.Message);
 }

>how will i reset my sr2.EndOfStream

You can use BaseStream property.

1.

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

OR
2.

sr2.BaseStream.Seek(0,SeekOrigin.Begin);
sr2.DiscardBufferedData();

adatapost, it work!!! thank you very very much... god bless...

Thanks. No problem, glad I could help. Please mark this thread as solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.