我正在尝试搜索他们居住的城镇中存储在文本文件中的客户帐户.有三个客户帐户都有不同的城镇.我认为该程序找到了正确的客户,因为它确实返回数据写入屏幕但不是全部.这些图像更好地解释了它.试图嵌入他们,但没有积分
文本文件内容:

当搜索住在利物浦的顾客时(尽管图像没有显示,但光标开始在DOB下面闪烁5行)

寻找贝尔法斯特客户.请注意,它会选择coreect DOB,但错误的客户编号和Surname.(我把评论中的链接放在一起不会让我发布超过2个链接)
下面是方法的代码::
static void FindTown(CustomerStruct[] CustomerDeats)
{
string City;
bool CityMatch = false;
Console.Clear();
begin:
try
{
Console.WriteLine("Please enter the customers Town ");
City = Console.ReadLine();
}
catch
{
Console.WriteLine("Failed. Please try again.");
goto begin;
}
var pathToTown = @"..\..\..\Files\Customer.txt";
using (StreamReader sr = new StreamReader(pathToTown))
while (!CityMatch)
{
{
RecCount = 0;
CustomerDeats[RecCount].Town = sr.ReadLine();
if (City == CustomerDeats[RecCount].Town)
{
Console.WriteLine("\n\n");
CityMatch = 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].Street);
CustomerDeats[RecCount].Town = sr.ReadLine();
Console.WriteLine(CustomerDeats[RecCount].Town);
CustomerDeats[RecCount].DOB = sr.ReadLine();
Console.WriteLine(CustomerDeats[RecCount].DOB);
Console.ReadKey();
}
RecCount++;
}
}
}
Run Code Online (Sandbox Code Playgroud)
如果我是你,我会做的第一件事就是更好地理解它的StreamReader运作方式.每次你ReadLine()在文件中调用你的位置都会推进另一行.我会重新考虑你的算法.也许有些东西:
如果您不了解如何读取文件,尝试在阅读文件时执行此操作将变得困难.
只是一个想法.:-)