read text file in loop and find a word

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • behrooz82
    New Member
    • Oct 2011
    • 1

    read text file in loop and find a word

    guys,
    I want to read a file and then find a word then print the line after that word.

    Code:
          static void Main()
          {
    
              StreamReader myFile = File.OpenText(@"test.txt");
              string myStringFile = myFile.ReadLine();
              bool condition = false;
    
              do
              {
    
                  if (myStringFile == "// #NAME") condition = true;
                  {
    
                      myStringFile = myFile.ReadLine();
                      Console.WriteLine(myStringFile);
    
                  }
    
              } while (myStringFile != null);
              Console.ReadKey();
    
    
          }
    without the do while loop it works and it just shows the the line after the first Match which is not my target.
    when I use do while it shows the whole text file!!

    any comment?
    I have attached the text file as well.
    here is the text file:

    // #Name
    // Behrooz 123_box_2000
    // #JOB TITLE
    // aaldkj
    // adlkfj a
    //
    // #Requirements
    // lab_121, lab_222, lab_312
    //
    // #Name
    // John 555_box_2010
    //
    // #JOB TITLE
    // aaldkj
    // adlkfj a
    //
    // #Requirements
    // lab_666, lab_819, lab_731
    Attached Files
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    Get rid of the condition stuff, you don't need it. Also, you need to separate your read line from your conditional. You always need to read a line or else you get stuck in an infinte loop once there's no match.

    Comment

    Working...