Searching for specific data in file using StreamReader C# then writing it to console window

StackOverflow https://stackoverflow.com/questions/23086164

  •  04-07-2023
  •  | 
  •  

I come to you with as I have exhausted myself searching Google and trying my own methods.

I have a struct made up of all strings which are stored in a text file. I want the user to be able to search for customer accounts in the text file by the customer's number and then the program should then return the customer information.

Below is the non-working search method I have created for the program. I get to the stage of this method as it allows me to enter the customer's number I want to search but after I press enter, I get nothing in return:

If you could tell me what is wrong in this code I'll be extremely grateful and relieved!

        //SEARCHING BY CUSTOMER NUMBER

    static void FindCustNo(CustomerStruct[] CustomerDeats)
    {
        String NumberSearch;
        bool CustomerNumberMatch = false;
        Console.Clear();

    begin:
        try
        {
            Console.WriteLine("\t\tPlease enter a customer number: ");
            NumberSearch = Console.ReadLine(); 
        }
        catch
        {
            Console.WriteLine("Failed. Please try again.");
            goto begin;
        }


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

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

                CustomerDeats[RecCount].CustomerNo = sr.ReadLine();                                   


                if (CustomerDeats[RecCount].CustomerNo == NumberSearch)
                    {
                        CustomerNumberMatch = true;

                        CustomerDeats[RecCount].CustomerNo = sr.ReadLine();
                        Console.WriteLine(CustomerDeats[RecCount].CustomerNo);

                        CustomerDeats[RecCount].Surname = sr.ReadLine();
                        Console.WriteLine(CustomerDeats[RecCount].Surname);

                        CustomerDeats[RecCount].Forename = sr.ReadLine();
                        Console.WriteLine(CustomerDeats[RecCount].Forename);

                        CustomerDeats[RecCount].Street = sr.ReadLine();
                        Console.WriteLine(CustomerDeats[RecCount].Town);

                        CustomerDeats[RecCount].Town = sr.ReadLine();
                        Console.WriteLine(CustomerDeats[RecCount].Town);

                        CustomerDeats[RecCount].DOB = sr.ReadLine();
                        Console.WriteLine(CustomerDeats[RecCount].DOB);  
                    }

                   RecCount++;   

                if (RecCount > 15)
                {
                    Console.WriteLine("No One Found");
                }
            }
        }

        Console.ReadKey();           
    }  
有帮助吗?

解决方案 2

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

        using (StreamReader sr = new StreamReader(pathToCust))

        ...

Seems to be the same problem someone else had just yesterday:

Why do you create a new StreamReader in every iteration?

Try this:

    var pathToCust = @"..\..\..\Files\Customer.txt";

    using (StreamReader sr = new StreamReader(pathToCust) {

        while (!CustomerNumberMatch)
        { 
            ...

其他提示

Please try to find in Array instead of file. Following lines of code will return Array of string from file

 string[] records = System.IO.File.ReadAllLines("filePath");
 foreach (var record in records)
   {
      string[] fields = record.Split(' ');//space
      if (customerIdToFind == fields[0])
        {
          Console.WriteLine("Customer ID:" + fields[0]);
          Console.WriteLine("Customer Name:" + fields[1]);
        }
    }

Do not do this for large file, it will load entire content to memory. so StreamReader is always good.

try this

[DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section,
                 string key, string def, StringBuilder retVal,
                int size, string filePath);
 /// <summary>
        /// read value from given section and key
        /// </summary>
        /// <param name="Section">string</param>
        /// <param name="Key">string</param>
        /// <returns>string</returns>
 public string IniReadValue(string Section, string Key)
        {
            StringBuilder temp = new StringBuilder(255);
            int i = GetPrivateProfileString(Section, Key, "", temp,
                                            255, this.path);
            return temp.ToString();

        }

To call this function See Below Code

string sBENCHNO = ini.IniReadValue("Sextion", "Key");
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top