Text written to the Console after reading from a TXT file using streamreader only partially appears

I am trying to find a customer account stored in a text file in the city in which they live. There are three customer accounts in which there are different cities. I think that the program finds the right client, since it returns data for writing to the screen, but not all. These images explain this better. Tried to introduce them, but have no points

Text file content:

enter image description here

When searching for a client who lives in Liverpool (although the image does not show it, the cursor starts flashing 5 lines below the DOB)

http://i1370.photobucket.com/albums/ag266/Aaron_McCauley/2_zpsb6561828.png

Search for a Belfast client. Note that it picks up the CoreOK DOB, but the wrong client number and last name. (Put the link in the comments, do not let me post more than two links).

Here is the method code:

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++;

                }

            }
    }
+4
5

, , , , . , , , , , ().

, . , , , , () :

using (StreamReader sr = new StreamReader(pathToTown))
{
    var tempLines = new List<string>();
    while (!CityMatch && !sr.EndOfStream)
    {

        string line = sr.ReadLine();

        if (City == line)
        {
            Console.WriteLine("\n\n");
            CityMatch = true;
            int count = tempLines.Count;
            Console.WriteLine("Customer No: {0}", tempLines[count-4]);
            Console.WriteLine("Customer Surname: {0}", tempLines[count - 3]);
            Console.WriteLine("Customer Forename: {0}", tempLines[count - 2]);
            Console.WriteLine("Customer Street: {0}", tempLines[count - 1]);
            Console.WriteLine("Customer Town: {0}", line);
            Console.WriteLine("Customer Day Of Birth: {0}", sr.ReadLine());
            Console.ReadKey();

        }else tempLines.Add(line);

    }
}

.BTW, while, . (!sr.EndOfStream), , , , , , CityMatch .

: LINQ:

 var town = Console.ReadLine();
 var record = File.ReadLines("filepath")
              .Where(x => x != string.Empty)
              .Select((line, idx) => new {line, idx})
              .GroupBy(x => x.idx/6)
              .FirstOrDefault(x => x.Any(e => e.line.Contains(town)));

if (record != null)
{
     var values = record.Select(x => x.line).ToArray();
     Console.WriteLine("Customer No: {0}", values[0]);
     Console.WriteLine("Customer Surname: {0}", values[1]);
     Console.WriteLine("Customer Forename: {0}", values[2]);
     Console.WriteLine("Customer Street: {0}", values[3]);
     Console.WriteLine("Customer Town: {0}", values[4]);
     Console.WriteLine("Customer Day Of Birth: {0}", values[5]);
}
+1

, , , - , StreamReader. , ReadLine(), . . , - :

  • .
  • - , .
  • , , (: Town == Liverpool).

, , .

.: -)

+5

, , 1 . . , . - :

string s = File.ReadAllText(pathToTown).Trim();
string[] customersUnparsed = s.Split(new[] { Environment.NewLine + Environment.NewLine },
    StringSplitOptions.RemoveEmptyEntries);
string[][] customers = Array.ConvertAll(customersUnparsed,
    c => c.Split(new[] { Environment.NewLine }, StringSplitOptions.None));

:

string[] customer = Array.Find(customers, c => c[4] == "Liverpool");

. , class struct , , .

+1

:

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

if (City == CustomerDeats[RecCount].Town)

, . , , , .

, , , .

+1

, , , . . EndOfStream. http://msdn.microsoft.com/en-us/library/vstudio/system.io.streamreader.endofstream

ReadLine . http://msdn.microsoft.com/en-us/library/vstudio/system.io.streamreader.readline

Town, . .

- "1000", - "", - "", "10 Mews Park", - "", "03/07/1989", "1001", - "", "", "10 ", - "", - "06.04.1969", "1002", "Whilshere", "", "9 ", ""

, , .

As you can see from the exit screen, which prints only the final recording DOB. This is due to the fact that, as shown above, all data in the file was removed from the stream.

Also, the final value of RecCount will always be 1. You reset to 0 at the beginning of each iteration of the loop.

0
source

Source: https://habr.com/ru/post/1536674/


All Articles