使用StreamReader C#在文件中搜索特定数据,然后将其写入控制台窗口

时间:2014-04-15 14:08:02

标签: c# search streamreader

我来找你,因为我已经筋疲力尽地搜索谷歌并尝试自己的方法。

我有一个由存储在文本文件中的所有字符串组成的结构。我希望用户能够通过客户编号在文本文件中搜索客户帐户,然后程序应该返回客户信息。

以下是我为该程序创建的非工作搜索方法。我进入这个方法的阶段,因为它允许我输入我想要搜索的客户号码,但在我按下回车后,我得不到任何回报:

如果你能告诉我这段代码有什么问题,我将非常感激和放心!

        //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();           
    }  

3 个答案:

答案 0 :(得分:2)

请尝试在Array中找到而不是文件。以下代码行将从文件

返回字符串数组
 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]);
        }
    }

不要对大文件执行此操作,它会将整个内容加载到内存中。所以StreamReader总是很好。

答案 1 :(得分:1)

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

        using (StreamReader sr = new StreamReader(pathToCust))

        ...

似乎是其他人只有yesterday的问题:

为什么要在每次迭代中创建一个新的StreamReader

试试这个:

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

    using (StreamReader sr = new StreamReader(pathToCust) {

        while (!CustomerNumberMatch)
        { 
            ...

答案 2 :(得分:0)

试试这个

string sBENCHNO = ini.IniReadValue("Sextion", "Key");

要调用此功能,请参阅下面的代码

length()