Search code examples
c#searchstreamreader

Why is streamreader only picking up first entry? c#


I am using the stream reader feature to search for a record in a text file then display that record on the console window. It is searching the text file by a question number (1-50). The only number it works with is question 1. It won't display any other question but it displays question 1 perfect. Here is a section of the code where the problem lies.

        static void Amending(QuestionStruct[] _Updating)
    {
        string NumberSearch;
        bool CustomerNumberMatch = false;
        Console.Clear();

   begin:
        try
        {
            Console.Write("\t\tPlease enter the question number you want to append: ");
            NumberSearch = Console.ReadLine();
            Console.ReadKey();
        }
        catch
        {
            Console.WriteLine("Failed. Please try again.");
            goto begin;
        }


        //finding the question number to replace

        while (!CustomerNumberMatch)
        {
           var pathToCust = @"..\..\..\Files\questions.txt";

            using (StreamReader sr = new StreamReader(pathToCust, true))
            {
                RecCount = 0;

                questions[RecCount].QuestionNum = sr.ReadLine();


                if (questions[RecCount].QuestionNum == NumberSearch)
                {
                    Console.Clear();

                    Console.WriteLine("Question Number: {0}", questions[RecCount].QuestionNum);

                    questions[RecCount].Level = sr.ReadLine();
                    Console.WriteLine("Level: {0}", questions[RecCount].Level);

                    questions[RecCount].Question = sr.ReadLine();
                    Console.WriteLine("Question: {0}", questions[RecCount].Question);

                    questions[RecCount].answer = sr.ReadLine();
                    Console.WriteLine("Answer: {0}", questions[RecCount].answer);

                    CustomerNumberMatch = true;


                }

                RecCount++;

                //sr.Dispose();
            }


        }

Solution

  • You are reopening the questions.txt file each time around your while loop, so it will start from the beginning of the file each time and never get past the first line (unless you are looking for question 1, when it will read the question detail lines). Instead, you need to loop inside of the using statement:

    var pathToCust = @"..\..\..\Files\questions.txt";
    using (StreamReader sr = new StreamReader(pathToCust, true))
    {
        while (!CustomerNumberMatch)
        {
            RecCount = 0;
            questions[RecCount].QuestionNum = sr.ReadLine();
            if (questions[RecCount].QuestionNum == NumberSearch)
            {
                Console.Clear();
                Console.WriteLine("Question Number: {0}", questions[RecCount].QuestionNum);
    
                questions[RecCount].Level = sr.ReadLine();
                Console.WriteLine("Level: {0}", questions[RecCount].Level);
    
                questions[RecCount].Question = sr.ReadLine();
                Console.WriteLine("Question: {0}", questions[RecCount].Question);
    
                questions[RecCount].answer = sr.ReadLine();
                Console.WriteLine("Answer: {0}", questions[RecCount].answer);
    
                CustomerNumberMatch = true;
            }
            RecCount++;
        }
    }
    

    Something still seems a little off with the code - e.g. you are only populating the question properties when you find a match with NumberSearch, but hopefully this gets you closer.