Как организовать поиск в файле и вывод в другой файл? - C#

Узнай цену своей работы

Формулировка задачи:

В файл записать информацию о сотрудниках некоторого предприятия: фамилия, домашний адрес, телефон, образование, оклад. Напечатать в другой файл список сотрудников, имеющих высшее образование. Ввод информации в файл сделал, но не получается организовать поиск и вывод в другой файл.
Листинг программы
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6. namespace Excellently_Console_
  7. {
  8. class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. int n;
  13. Console.WriteLine("Введите количество сотрудников");
  14. n = Convert.ToInt32(Console.ReadLine());
  15. string[] Text = new string[n];
  16. Console.WriteLine("Введите фамилию, домашний адрес, телефон, образование, оклад(через пробел):");
  17. for (int i = 0; i < n; i++)
  18. {
  19. Text[i] = Console.ReadLine();
  20. File.AppendAllText(@"D:\FILE.txt", Text[i], Encoding.GetEncoding(1251));
  21. File.AppendAllText(@"D:\FILE.txt", "\r\n");
  22. }
  23. }
  24. }
  25. }

Решение задачи: «Как организовать поиск в файле и вывод в другой файл?»

textual
Листинг программы
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Drawing;
  6. using System.IO;
  7.  
  8.  
  9. namespace MyPractice
  10. {
  11.     class Program
  12.     {
  13.  
  14.         public struct Personal
  15.         {
  16.             public string sorName;
  17.             public string adress;
  18.             public string phoneNumber;
  19.             public string education;
  20.             public double salary;
  21.  
  22.             public Personal (  string sorName,string adress,string phoneNumber,
  23.            string education, double salary )
  24.              {
  25.              this.sorName= sorName;
  26.              this.adress=adress;
  27.              this.phoneNumber = phoneNumber;
  28.              this.education = education;
  29.              this.salary = salary;
  30.             }
  31.  
  32.             public override string ToString()
  33.             {
  34.                 return sorName+","+adress+","+phoneNumber+","+education+","+salary;
  35.             }
  36.         }
  37.  
  38.  
  39.         public static Personal InputPerson()
  40.         {
  41.             Personal person= new Personal ();
  42.             Console.WriteLine("Input SorName: ");
  43.             person.sorName= Console.ReadLine();
  44.             Console.WriteLine("Input Adress: ");
  45.             person.adress = Console.ReadLine();
  46.             Console.WriteLine("Input PhoneNumber: ");
  47.             person.phoneNumber = Console.ReadLine();
  48.             Console.WriteLine("Input Education: ");
  49.             person.education = Console.ReadLine();
  50.             Console.WriteLine("Input salary: ");
  51.             person.salary = double.Parse(Console.ReadLine());
  52.             return person;
  53.         }
  54.  
  55.  
  56.  
  57.         public static List<Personal> Input()
  58.            
  59.         {
  60.             bool z = true;
  61.             List<Personal> myList = new List<Personal>();
  62.             while (z)
  63.             {
  64.                 Console.Clear();
  65.                 Console.WriteLine("Input person press 1");
  66.                 Console.WriteLine("Exit press 2");
  67.                 string str = Console.ReadLine();
  68.                 switch (str)
  69.                 {
  70.                        
  71.                     case "1": { Console.Clear(); myList.Add(InputPerson()); break; }
  72.                     case "2": { Console.Clear(); z = false; break; }
  73.                     default:
  74.                         {
  75.                             Console.Clear(); Console.WriteLine("Make your choice correctly!!!");
  76.                             break;
  77.                         }
  78.                 }
  79.  
  80.             }
  81.             return myList;
  82.         }
  83.  
  84.         public static void FileWrite(List<Personal> myList)
  85.         {
  86.             using (StreamWriter sw = new StreamWriter(@"d:\myFile.txt"))
  87.             {
  88.                 foreach (var i in myList)
  89.                 {
  90.                     sw.WriteLine(i);
  91.                 }
  92.  
  93.             }
  94.         }
  95.  
  96.         public static List<Personal> SearchAndWrite(List<Personal> myList)
  97.         {
  98.             List<Personal> myList1 = new List<Personal>();
  99.  
  100.             foreach (var i in myList)
  101.             {
  102.                 if (i.education == "высшее")
  103.                 {
  104.                     myList1.Add(i);
  105.                 }
  106.             }
  107.  
  108.          
  109.           return myList1;
  110.         }
  111.  
  112.         public static void NewFileWrite(List<Personal> myList1)
  113.         {
  114.             using (StreamWriter sw = new StreamWriter(@"d:\myFile1.txt"))
  115.             {
  116.                 foreach (var i in myList1)
  117.                 {
  118.                     sw.WriteLine(i);
  119.                 }
  120.             }
  121.  
  122.  
  123.         }
  124.  
  125.         static void Main(string[] args)
  126.         {
  127.                
  128.             List<Personal> myList=Input();
  129.             foreach ( var i in myList)
  130.             {
  131.                 Console.WriteLine(i);
  132.             }
  133.             FileWrite(myList);
  134.             List<Personal> myList1 = SearchAndWrite(myList);
  135.             NewFileWrite(myList1);
  136.             Console.ReadKey();
  137.         }
  138.     }
  139. }

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

8   голосов , оценка 4.125 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут
Похожие ответы