title: I'm reading line by line from a text file but in the messagebox.show I see the same first line all the time
body: <p>In the constructor I did:</p>
  
  <pre><code>binFileToRead = @"d:\DenyTextHaunted.bin";
  r = new StreamReader(binFileToRead);
  f = r.ReadToEnd();
  translatedFile = @"d:\binText_1.txt";
  h = new StreamReader(translatedFile);
  test();
  </code></pre>
  
  <p>Now the code in test()</p>
  
  <pre><code>private void test()
  {
              StreamWriter w = new StreamWriter(@"d:\testFile.txt");
              w.AutoFlush = true;
              int currentLength;
              int currentIndex;
              int lastIndex = 0;
              string startTag = "&lt;Text&gt;";
              string endTag = "&lt;/Text&gt;";
              int startTagWidth = startTag.Length;
              //int endTagWidth = endTag.Length;
              int index = 0;
              while ((line = h.ReadLine()) != null)
              {
                  while (true)
                  {
  
                      index = f.IndexOf(startTag, index);
                      if (index == -1)
                      {
                          //startTag not found, but endTag may still be found
                          index = f.IndexOf(endTag, lastIndex - 1);
                          if (index != -1)
                          {
                              w.Write(f.Substring(lastIndex, endTag.Length));
                          }
  
                          break;
                      }
                      // else more to do - index now is positioned at first character of startTag    
                      int start = index + startTagWidth;
  
                      if (start &gt;= f.Length)
                          break;
  
                      currentIndex = start;
                      index = f.IndexOf(endTag, start + 1);
                      if (index == -1)
                      {
                          break;
                      }
                      // found the endTag    
                      string g = f.Substring(start, index - start - 1);
                      currentLength = index - start;
                      string hh = f.Substring(lastIndex, currentIndex - lastIndex);
                      w.Write(hh);
                      w.Write(line);
                      MessageBox.Show(line.ToString());
                      listBox2.Items.Add(hh);
                      lastIndex = currentIndex + currentLength;
                      listBox1.Items.Add(g);
                  }
                  w.Close();
              }
  
          }
  </code></pre>
  
  <p>What I did in test() so far is reading one file to the end and then taking out all the file content without the text between the tags <code>&lt;Text&gt;</code> and <code>&lt;/Text&gt;</code>  </p>
  
  <p>And wrote this to a new text file.</p>
  
  <p>So the new text file for example looks like:</p>
  
  <pre><code>hi everyone &lt;Text&gt;&lt;/Text&gt; and you are...
  &lt;Text&gt;&lt;/Text&gt; by and hello
  </code></pre>
  
  <p>Now what I wanted to do is to read line by line from another file which is variable <code>h</code> and I added this while (<code>(line = h.ReadLine()) != null</code>)</p>
  
  <p>Then I'm writing each line to the place in between the <code>&lt;Text&gt;&lt;/Text&gt;</code></p>
  
  <pre><code>w.Write(line);
  </code></pre>
  
  <p>But in the <code>MessageBox.Show(line.ToString());</code> I see the first line of the text file all the time.
  I'm reading and repeating all the time showing the first line only.</p>
  
  <p>Why doesn't it show me the next file in the next iteration ?</p>
  
  <p>Thanks.</p>
  
  <hr>
  
  <p>Guffa it dosent working good. I used your repair sample using one while loop to read line by line but then the written new file is missing some lines from the end. Im not sure if the problem is in the written or in the reading to the end in the constructor. Or maybe the loop isnt good.</p>
  
  <p>What i wanted to do is to read some of F untill the point of  the read some of H one line and then put this line between the first  then that F will keep reading to the next  then reading from H the next second line and then put the second line in the second place of </p>
  
  <p>So its working in general the concept is working but technicaly it dosent its reading or writing to the new file almost to the end but missing few lines.</p>
  
  <pre><code>private void test()
          {
              StreamWriter w = new StreamWriter(@"d:\testFile.txt");
              w.AutoFlush = true;
              int currentLength;
              int currentIndex;
              int lastIndex = 0;
              string startTag = "&lt;Text&gt;";
              string endTag = "&lt;/Text&gt;";
              int startTagWidth = startTag.Length;
              //int endTagWidth = endTag.Length;
              int index = 0;
              while ((line = h.ReadLine()) != null)
              {
  
                  //while (true)
                  //{
  
                      index = f.IndexOf(startTag, index);
                      if (index == -1)
                      {
                          //startTag not found, but endTag may still be found
                          index = f.IndexOf(endTag, lastIndex - 1);
                          if (index != -1)
                          {
                              w.Write(f.Substring(lastIndex, endTag.Length));
                          }
  
                          break;
                      }
                      // else more to do - index now is positioned at first character of startTag    
                      int start = index + startTagWidth;
  
                      if (start &gt;= f.Length)
                          break;
  
                      currentIndex = start;
                      index = f.IndexOf(endTag, start + 1);
                      if (index == -1)
                      {
                          break;
                      }
                      // found the endTag    
                      string g = f.Substring(start, index - start - 1);
                      currentLength = index - start;
                      string hh = f.Substring(lastIndex, currentIndex - lastIndex);
                      w.Write(hh);
                      w.Write(line);
                      listBox2.Items.Add(hh);
                      lastIndex = currentIndex + currentLength;
                      listBox1.Items.Add(g);
                  //}
                  //w.Close();
              }
              w.Close();
          }
  </code></pre>
  
  <p>f is a file in the constructor im reading to the end: </p>
  
  <pre><code>binFileToRead = @"d:\DenyTextHaunted.bin";
  r = new StreamReader(binFileToRead);
  f = r.ReadToEnd();
  </code></pre>
  
  <p>And h is another text file im reading in the constructor but no to the end :</p>
  
  <pre><code>translatedFile = @"d:\binText_1.txt";
  h = new StreamReader(translatedFile);
  </code></pre>
  
  <p>Then in the function test()</p>
  
  <p>im creating a new text file that should contain all the content of the file F without the text between the tags  and </p>
  
  <p>And im writing to this file also the h file lines line by line:</p>
  
  <p>First its reading some of F until the first  and then its reading one line from H and put the line between the </p>
  
  <p>And so on.</p>
  
  <p>But for some reason when im editing the new text file testFile.txt i see that it didnt write to the end.</p>
  
  <p>The last line is for example like this:</p>
  
  <p>Hmm smells not badly, but you do not have a cirque-red and the following Ã ¼r micht please... grr.</p>
  
  <p>But there is no  in the end and i checked the original file there are more few lines after this line.</p>
  
  <p>So why its reading the original file or writing to the new file only untill this part ?</p>
  
  <p>Maybe since the loop im using is finishing when its finish to read all lines from the H file ?</p>
  
  <p>Before it i had another loop i used it was just While(true){}</p>
  
  <p>But then it didnt read line by line from H</p>
  
  <p>Im not sure why its not writing or reading to the end. </p>
  
  <p>The concept is working but technicaly its not getting to the end.</p>
  
  <p>Here is example of how the H im reading is looks like:</p>
  
  <p>The barrel also does not help me here.
  The barrel makes here really no sense.
  Here pity also makes the barrel no sense.
  The barrel makes no sense with the poster.
  Only I must create good idea, it anyhow after drau Ã en...
  The ship k Ã ¶nnte are used anyhow, around the barrel to entz Ã ¼nden. But not under water.
  First not.</p>
  
  <p>This is the file i want to readl ine by line.</p>
  
  <p>The other file had another text between  and now i want to take line by line as explained above reading some of this then reading some of this put a line and so on.</p>
  
  <p>Thanks.</p>