I am having trouble finding words in a text file in C #.
I want to find the word that is entered into the console, and then display the entire line in which the word was found on the console.
In my text file I have:
Stephen Haren, December, 9.4055551235
Laura Clausing, January 23, 4054447788
William Connor, December 13, 123456789
Kara Marie, October 23,1593574862
Audrey Carrit, January 16, 2008.
Sebastian Baker, October, 23,9184569876
So, if I enter December, I want him to display Stephen Haren, 9.4055551235 and William Connor, December 13, 123456789.
I was thinking about using substrings, but I decided there should be an easier way.
My code after the given answer:
using System;
using System.IO;
class ReadFriendRecords
{
public static void Main()
{
FileStream inFile = new FileStream(@"H:\C#\Chapter.14\FriendInfo.txt", FileMode.Open, FileAccess.Read);
StreamReader reader = new StreamReader(inFile);
string record;
string input;
Console.Write("Enter Friend Birth Month >> ");
input = Console.ReadLine();
try
{
record = reader.ReadLine();
while (record != null)
{
if (record.Contains(input))
{
Console.WriteLine(record);
}
record = reader.ReadLine();
}
}
finally
{
reader.Close();
inFile.Close();
}
Console.ReadLine();
}
}