Gabe
Gabe

Reputation: 35

Searching for a word in a text file in C#

I have a small txt file containing three lines of text. I'm trying to create a programme in C# that gets a word from a user and searches for that word in the txt file. If the word is found, I'm wanting to record and display what lines of the txt file the word was found on. The variable int position records if the word is found or not, but I can't work out how to record what lines the word is found on. How would I do this? Here is my code:

class Program {
    static void Main(string[] args) {
        Console.Write("please enter a file to search for");
        string fileResponse = Console.ReadLine();
        Console.Write("please enter a word to search for in the file");
        string wordResponse = Console.ReadLine();
        StreamReader myfile = File.OpenText(fileResponse);
        string line = myfile.ReadLine();
        int position = line.IndexOf(wordResponse);
        int count = 0; //counts the number of times wordResponse is found.
        int lineNunmber = 0;

            while (line != null) {
                if (position != -1) {
                    count++;
                }
                line = myfile.ReadLine();
            }
         if (count == 0) {
            Console.WriteLine("your word was not found!");
         } else {
            Console.WriteLine("Your word was found " + count + " times!" + position);
         }
         Console.ReadLine();
    }
}

Upvotes: 1

Views: 4660

Answers (2)

Fai
Fai

Reputation: 389

I'm assuming you want to print the matching lines and the associate line number. Currently you only remember the number of times you matched. The position you print at the end is wrong, and most likely -1 (unless your last match is on the last line). If you don't need to do anything else with the matches, easiest way would be to print when you find it.

(Also, you're not closing the file you're opening)

using (StreamReader myFile = File.OpenText(fileResponse))
{
    int count = 0; //counts the number of times wordResponse is found.
    int lineNumber = 0;
    while(!myFile.EndOfStream)
    {
        string line = myFile.ReadLine();
        lineNumber++;
        int position = line.IndexOf(wordResponse);
        if (position != -1) {
            count++;
            Console.WriteLine("Match #{0} {1}:{2}", count, lineNumber, line)  
        }
}

if (count == 0) {
    Console.WriteLine("your word was not found!");
} else {
    Console.WriteLine("Your word was found " + count + " times!");
}
Console.ReadLine();

edit:spelling

Upvotes: 2

Grant Peters
Grant Peters

Reputation: 7825

This looks like a homework task so I'm only going to give you some hints:

  1. Every time you call "myfile.ReadLine()", you have moved to the next line of the file. You might want to track how many lines you have read.

  2. To record a list of values, you can use a List<Type> variable.

    Example:

    // Create the list
    List<int> myListOfIntegers = new List<int>();
    
    // Add some values
    myListOfIntegers.Add(2);
    myListOfIntegers.Add(5);
    myListOfIntegers.Add(3);
    
    // Iterate through the list
    foreach(int item in myListOfIntegers)
    {
        Console.WriteLine("Item Value: " + item);
    }
    

    Result:

    Item Value: 2
    Item Value: 5
    Item Value: 3
    

Upvotes: 0

Related Questions