I am trying to search for a word in a text file that I am reading into C#.
As of right now it keeps on showing the word is at line zero when it is not.
What am I doing wrong in the code?
Also, how would I make it count the word that I search for so that it can show the amount of occurrence?
string line;
int counter = 0;
Console.WriteLine("Enter a word to search for: ");
var text = Console.ReadLine();
string file = "newfile.txt";
StreamReader myFile = new StreamReader(file);
Console.WriteLine("\n");
while ( (line = myFile.ReadLine()) != null )
{
if(line.Contains(text))
{
break;
}
counter++;
}
Console.WriteLine("Line number: {0}", counter);
myFile.Close();
Console.ReadLine();